#!/usr/bin/env python3

"""
Clean Market Cap Validation CLI

Validates market cap claims from pitch deck slides using RAG search.
Reports are automatically organized in the processed/ directory.
"""

import sys
import os
import argparse
from modules.document_validator import (
    validate_document_claims,
    validate_all_processed_documents
)


def main():
    parser = argparse.ArgumentParser(
        description="Validate market cap claims from pitch deck slides using RAG search"
    )
    
    parser.add_argument(
        '--file', '-f',
        help='Path to JSON file containing slide data'
    )
    
    parser.add_argument(
        '--document', '-d',
        help='Document name for organized reporting'
    )
    
    parser.add_argument(
        '--all',
        action='store_true',
        help='Validate all documents in processed/ folder'
    )
    
    parser.add_argument(
        '--no-save',
        action='store_true',
        help='Do not save validation report to file'
    )
    
    parser.add_argument(
        '--api-key',
        help='OpenRouter API key (or set OPENROUTER_API_KEY environment variable)'
    )
    
    args = parser.parse_args()
    
    # Get API key
    api_key = args.api_key or os.getenv('OPENROUTER_API_KEY')
    if not api_key:
        print("❌ Error: OpenRouter API key required")
        print("   Set OPENROUTER_API_KEY environment variable or use --api-key")
        sys.exit(1)
    
    try:
        print("🔍 Market Cap Validation with RAG Search")
        print("=========================================")
        
        if args.all:
            print("📁 Validating all documents in processed/ folder")
            results = validate_all_processed_documents(api_key=api_key)
            
            print(f"\n✅ Validation Complete!")
            print(f"📊 Processed {len(results)} documents:")
            
            for doc_name, doc_results in results.items():
                if 'error' in doc_results:
                    print(f"   ❌ {doc_name}: {doc_results['error']}")
                else:
                    summary = doc_results['summary']
                    print(f"   ✅ {doc_name}: {summary['total_claims']} claims, {summary['accuracy_rate']:.1f}% accurate")
                    if doc_results['report_filename']:
                        print(f"      📄 Report: {doc_results['report_filename']}")
        
        elif args.file:
            document_name = args.document or "Unknown-Document"
            print(f"📁 Validating from file: {args.file}")
            
            import json
            with open(args.file, 'r', encoding='utf-8') as f:
                slide_data = json.load(f)
            
            results = validate_document_claims(
                document_name, 
                slide_data,
                api_key=api_key,
                save_report=not args.no_save
            )
            
            # Display results
            summary = results['summary']
            print(f"\n✅ Validation Complete!")
            print(f"📊 Results Summary:")
            print(f"   - Total Claims Found: {summary['total_claims']}")
            print(f"   - Accurate Claims: {summary['accurate_claims']}")
            print(f"   - Inaccurate Claims: {summary['inaccurate_claims']}")
            print(f"   - Accuracy Rate: {summary['accuracy_rate']:.1f}%")
            
            if results['report_filename']:
                print(f"📄 Detailed report saved to: {results['report_filename']}")
        
        else:
            print("📁 Validating all documents in processed/ folder (default)")
            results = validate_all_processed_documents(api_key=api_key)
            
            print(f"\n✅ Validation Complete!")
            print(f"📊 Processed {len(results)} documents:")
            
            for doc_name, doc_results in results.items():
                if 'error' in doc_results:
                    print(f"   ❌ {doc_name}: {doc_results['error']}")
                else:
                    summary = doc_results['summary']
                    print(f"   ✅ {doc_name}: {summary['total_claims']} claims, {summary['accuracy_rate']:.1f}% accurate")
                    if doc_results['report_filename']:
                        print(f"      📄 Report: {doc_results['report_filename']}")
        
    except Exception as e:
        print(f"❌ Error: {e}")
        sys.exit(1)


if __name__ == "__main__":
    main()
