import sys
import os
import argparse

# Add project root to path so we can import utils.json_extractor
current_dir = os.path.dirname(os.path.abspath(__file__))
project_root = os.path.dirname(os.path.dirname(current_dir))
sys.path.append(project_root)

from utils.json_extractor.extractor import SchemaExtractor

def main():
    parser = argparse.ArgumentParser(description="Extract JSON schema from 5E Arena data.")
    parser.add_argument("--input", default="output_arena", help="Input directory containing iframe_network.json files")
    parser.add_argument("--output-json", default="output_reports/schema_full.json", help="Output JSON report path")
    parser.add_argument("--output-md", default="output_reports/schema_summary.md", help="Output Markdown summary path")
    parser.add_argument("--output-csv", default="output_reports/schema_flat.csv", help="Output CSV flat report path")
    
    args = parser.parse_args()
    
    print(f"Starting extraction from {args.input}...")
    extractor = SchemaExtractor()
    extractor.process_directory(args.input)
    
    # Ensure output directory exists
    os.makedirs(os.path.dirname(args.output_json), exist_ok=True)
    os.makedirs(os.path.dirname(args.output_md), exist_ok=True)
    
    extractor.export_report(args.output_json)
    extractor.export_markdown_summary(args.output_md)
    extractor.export_csv_summary(args.output_csv)
    print("Done.")

if __name__ == "__main__":
    main()
