#!/usr/bin/env python3
"""
Script 07: Validation script for the Causality Atlas.
Checks: cross-reference integrity, file existence, DOI validity.
"""
import os, json, re

BASE = os.path.expanduser("~/Documents/Causality/docs")
SCRIPTS = os.path.expanduser("~/Documents/Causality/scripts")
REFS = os.path.expanduser("~/Documents/Causality/references")

errors = []
warnings = []

def check(condition, msg, kind="error"):
    if not condition:
        if kind == "error":
            errors.append(msg)
        else:
            warnings.append(msg)

def find_md_links(text, filepath):
    """Find all .md cross-references in a markdown file."""
    links = re.findall(r'\(\.?/?(?:[^)]*?\.md)(?:#[^)]*)?\)', text)
    resolved = []
    for link in links:
        target = link.strip('()')
        target = re.sub(r'#.*$', '', target)
        target = os.path.basename(target)
        resolved.append(target)
    return resolved

# ---- 1. File completeness ----
print("=== Phase 1: File Completeness ===")
chapter_files = [f for f in os.listdir(BASE) if f.endswith('.md')]
print(f"Found {len(chapter_files)} markdown files in docs/")

required = [
    '00-MASTER-INDEX.md', '00-CASE-FOR-CAUSALITY.md', '01-AXIOMS.md',
    '02-PHILOSOPHICAL-FOUNDATIONS.md', '03-FISHER-RANDOMIZATION.md',
    '04-HILL-EPIDEMIOLOGY.md', '05-NEYMAN-RUBIN-POTENTIAL-OUTCOMES.md',
    '06-PEARL-SCM-DO-CALCULUS.md', '07-ROBINS-G-METHODS.md',
    '08-CAUSAL-DISCOVERY.md', '09-ECONOMETRIC-QUASI-EXPERIMENTS.md',
    '10-CAUSAL-MACHINE-LEARNING.md', '11-MEDIATION-MECHANISMS.md',
    '12-SENSITIVITY-ANALYSIS.md', '13-BAYESIAN-CAUSALITY.md',
    '14-TIME-VARYING-CAUSAL.md', '15-MISSING-DATA-AND-COMPLIANCE.md',
    '16-SOFTWARE-AND-TOOLS.md', '17-BIBLIOGRAPHY.md',
    '18-METHOD-COMPARISON.md', '19-GLOSSARY.md',
    'NAVIGATOR.md', 'STYLE-GUIDE.md'
]
for f in required:
    check(os.path.exists(os.path.join(BASE, f)), f"Missing: {f}")

# Check line counts
print("\n--- Line Counts ---")
for f in sorted(chapter_files):
    path = os.path.join(BASE, f)
    lines = sum(1 for _ in open(path))
    print(f"  {f}: {lines} lines")

# ---- 2. Cross-reference integrity ----
print("\n=== Phase 2: Cross-Reference Integrity ===")
all_files = set(chapter_files)

for md_file in chapter_files:
    path = os.path.join(BASE, md_file)
    with open(path) as f:
        content = f.read()
    links = find_md_links(content, md_file)
    for target in set(links):
        if target and target not in all_files:
            # Check if it's a section anchor
            if '#' not in target:
                errors.append(f"{md_file} → {target}: target file not found")

# ---- 3. Script validation ----
print("\n=== Phase 3: Script Inventory ===")
r_scripts = [f for f in os.listdir(os.path.join(SCRIPTS, 'causal_estimators_R'))
             if f.endswith('.R')]
py_scripts = [f for f in os.listdir(os.path.join(SCRIPTS, 'causal_estimators_python'))
              if f.endswith('.py')]
print(f"R scripts: {len(r_scripts)} ({', '.join(sorted(r_scripts))})")
print(f"Python scripts: {len(py_scripts)} ({', '.join(sorted(py_scripts))})")

# ---- 4. References ----
print("\n=== Phase 4: Reference Files ===")
csl_path = os.path.join(REFS, 'references.csl.json')
ris_path = os.path.join(REFS, 'references.ris')
check(os.path.exists(csl_path), "Missing: references.csl.json")
check(os.path.exists(ris_path), "Missing: references.ris")

if os.path.exists(csl_path):
    with open(csl_path) as f:
        refs = json.load(f)
    check(len(refs) > 10, f"Only {len(refs)} references in CSL file")
    for r in refs:
        check('id' in r, f"Reference missing 'id': {r.get('title', '?')[:40]}")
        check('author' in r, f"Reference missing 'author': {r.get('id', '?')}")

# ---- 5. Output files ----
print("\n=== Phase 5: Output Files ===")
fig_dir = os.path.expanduser("~/Documents/Causality/outputs/figures")
svg_files = [f for f in os.listdir(fig_dir) if f.endswith('.svg')]
print(f"Figure SVGs: {len(svg_files)} ({', '.join(sorted(svg_files))})")

# ---- SUMMARY ----
print("\n" + "=" * 50)
print("VALIDATION SUMMARY")
print("=" * 50)
if errors:
    print(f"\nERRORS ({len(errors)}):")
    for e in errors:
        print(f"  ❌ {e}")
else:
    print("\n✅ No cross-reference errors!")

if warnings:
    print(f"\nWARNINGS ({len(warnings)}):")
    for w in warnings:
        print(f"  ⚠️  {w}")

print(f"\n📊 Total docs: {len(chapter_files)} markdown files")
print(f"📊 Total scripts: {len(r_scripts)} R + {len(py_scripts)} Python")
print(f"📊 Total figures: {len(svg_files)} SVGs")
if os.path.exists(csl_path):
    print(f"📊 References: {len(refs)} entries in CSL JSON")
print(f"\nOverall: {'✅ PASS' if not errors else '❌ ' + str(len(errors)) + ' error(s) found'}")
