#!/usr/bin/env python3
import json
import yaml
from pathlib import Path
from datetime import datetime, timezone

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

def main():
    resolver_path = ROOT / "Temp" / "canonical_artifact_resolver_v1.json"
    manifest_path = ROOT / "runtime" / "active_artifact_manifest.yaml"
    canonical_manifest_path = ROOT / "artifacts" / "canonical_manifest.yaml"

    if not resolver_path.exists():
        print(f"Resolver output not found: {resolver_path}")
        return 1
    
    resolver_data = json.loads(resolver_path.read_text(encoding="utf-8"))
    canonical_map = resolver_data.get("canonical_map", {})

    if not manifest_path.exists():
        print(f"Active manifest not found: {manifest_path}")
        return 1

    manifest = yaml.safe_load(manifest_path.read_text(encoding="utf-8"))
    
    # Update manifest rows based on canonical map
    rows = manifest.get("manifest_rows", [])
    updated_count = 0
    
    # Map concepts to formula_ids in manifest
    concept_to_formula = {
        "smart_cash_recovery": "smart_cash_recovery_v7", # Need to check if this is the ID
        "final_execution_decision": "final_execution_gate",
        "prediction_accuracy_harness": "prediction_match_rate_pct"
    }
    
    # Actually, the manifest rows have formula_id like 'smart_cash_recovery_v7'
    # We should rename them to concept names or keep them?
    # The TODO says "remove drift".
    
    new_rows = []
    for row in rows:
        fid = row["formula_id"]
        # Match by prefix or known map
        matched_concept = None
        for concept in canonical_map:
            if fid.startswith(concept):
                matched_concept = concept
                break
        
        if matched_concept:
            old_art = row["active_artifact"]
            new_art = f"Temp/{canonical_map[matched_concept]}"
            if old_art != new_art:
                print(f"Updating {fid}: {old_art} -> {new_art}")
                row["active_artifact"] = new_art
                # If the ID had a version suffix, update it to the new one if appropriate
                # but 'smart_cash_recovery_v7' is used as a formula_id key. 
                # Let's keep the formula_id as is for now but update the artifact.
                updated_count += 1
        new_rows.append(row)

    manifest["manifest_rows"] = new_rows
    manifest["generated_at"] = datetime.now(timezone.utc).isoformat()
    
    # Update canonical_manifest.yaml concepts too
    if canonical_manifest_path.exists():
        c_man = yaml.safe_load(canonical_manifest_path.read_text(encoding="utf-8"))
        # (Assuming it's already mostly correct from build_canonical_artifact_resolver)
    
    manifest_path.write_text(yaml.dump(manifest, sort_keys=False, allow_unicode=True), encoding="utf-8")
    print(f"Successfully synced active manifest. Updated {updated_count} rows.")
    return 0

if __name__ == "__main__":
    main()
