#!/usr/bin/env python3
"""
Generate Broken References Report
Creates a detailed report of broken markdown links for manual fixing
"""

import json
import re
from pathlib import Path
from collections import defaultdict

def load_inconsistencies():
    """Load content inconsistencies"""
    with open('CONTENT_INCONSISTENCIES.json', 'r') as f:
        return json.load(f)

def generate_report():
    """Generate broken references report"""
    data = load_inconsistencies()
    
    broken_refs = [inc for inc in data['inconsistencies'] if inc['type'] == 'broken_reference']
    
    # Group by file
    by_file = defaultdict(list)
    for ref in broken_refs:
        by_file[ref['file']].append(ref['issue'])
    
    # Generate report
    report = []
    report.append("# Broken References Report")
    report.append("")
    report.append(f"**Total Broken References**: {len(broken_refs)}")
    report.append(f"**Files Affected**: {len(by_file)}")
    report.append("")
    report.append("## Summary")
    report.append("")
    report.append("This report lists all broken markdown cross-references.")
    report.append("Most broken references are likely due to files being moved during cleanup.")
    report.append("")
    report.append("## Broken References by File")
    report.append("")
    
    for file_path in sorted(by_file.keys()):
        report.append(f"### {file_path}")
        report.append("")
        for issue in by_file[file_path]:
            report.append(f"- {issue}")
        report.append("")
    
    report.append("## Common Patterns")
    report.append("")
    report.append("### Files Moved to reports/")
    report.append("- Status reports → `reports/status/`")
    report.append("- Analysis reports → `reports/analyses/`")
    report.append("- VMID reports → `reports/`")
    report.append("")
    report.append("### Files Moved to docs/")
    report.append("- Configuration guides → `docs/04-configuration/`")
    report.append("- Troubleshooting guides → `docs/09-troubleshooting/`")
    report.append("- Quick start guides → `docs/01-getting-started/`")
    report.append("- References → `docs/11-references/`")
    report.append("")
    report.append("### Files Archived")
    report.append("- Timestamped files → `reports/archive/2026-01-05/`")
    report.append("- rpc-translator-138 temp files → `rpc-translator-138/docs/archive/`")
    report.append("")
    
    return "\n".join(report)

if __name__ == '__main__':
    report = generate_report()
    with open('BROKEN_REFERENCES_REPORT.md', 'w') as f:
        f.write(report)
    print("✅ Broken references report generated: BROKEN_REFERENCES_REPORT.md")
    print(f"   Total broken references: {len([inc for inc in json.load(open('CONTENT_INCONSISTENCIES.json'))['inconsistencies'] if inc['type'] == 'broken_reference'])}")
