import json
import pandas as pd

with open("/home/hieutt50/projects/DSOL/cross_references.json", 'r') as f:
    refs = json.load(f)

print("## Cross-Reference Analysis Findings")
print("")

if not refs:
    print("No significant cross-references found.")
else:
    df = pd.DataFrame(refs)
    
    # Group by Source File
    print("### References by Source File")
    for source, group in df.groupby("source_file"):
        print(f"- **{source}** references:")
        for _, row in group.iterrows():
            print(f"  - `{row['referenced_key']}` ({row['key_type']}) -> Linked to {row['target_context']}")
        print("")

    # Group by Key (What is being referenced?)
    print("### Most Referenced Items")
    counts = df['referenced_key'].value_counts()
    for key, count in counts.items():
        print(f"- **{key}**: {count} references")
