#!/usr/bin/env python3
import json
import argparse
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--json", default="Temp/artifact_chain_hash_v4.json")
    args = parser.parse_args()
    
    path = ROOT / args.json
    if not path.exists():
        print(f"Chain file not found: {path}")
        return 1
        
    data = json.loads(path.read_text(encoding="utf-8"))
    chain = data.get("chain", [])
    length = data.get("chain_length", 0)
    
    print(f"ARTIFACT CHAIN LENGTH: {length}")
    if length < 4:
        print("FAIL: Artifact chain is too short (min 4 required)")
        return 1
    
    # Simple check: each node must have a sha256
    for node in chain:
        if not node.get("sha256"):
            print(f"FAIL: Node {node.get('path')} is missing hash")
            return 1
            
    print("PASS: Artifact hash chain validated")
    return 0

if __name__ == "__main__":
    main()
