"""
Automated testing of sample files with V2 extraction pipeline.
"""
import time
import json
from pathlib import Path
from src.extraction_v2.pipeline import ExtractionPipelineV2

def test_file(file_path: str) -> dict:
    """Test extraction on a single file."""
    result = {
        "file_path": file_path,
        "file_name": Path(file_path).name,
        "status": "unknown",
        "error": None,
        "records_extracted": 0,
        "tables_found": 0,
        "processing_time_seconds": 0,
        "sheet_names_extracted": [],
        "warnings": []
    }

    print(f"\n{'='*80}")
    print(f"Testing: {result['file_name']}")
    print('='*80)

    start_time = time.time()

    try:
        # Run V2 extraction
        pipeline = ExtractionPipelineV2()
        extraction_result = pipeline.process_document_sync(None, file_path)
        result["processing_time_seconds"] = round(time.time() - start_time, 2)

        if not extraction_result.success:
            result["status"] = "error"
            result["error"] = extraction_result.error
            print(f"✗ ERROR: {result['error']}")
            return result

        result["records_extracted"] = extraction_result.record_count

        # Analyze records
        sheet_names = set()
        table_ids = set()

        for record in extraction_result.records:
            if record.sheet_name:
                sheet_names.add(record.sheet_name)
            if record.table_id:
                table_ids.add(record.table_id)

        result["sheet_names_extracted"] = sorted(list(sheet_names))
        result["tables_found"] = len(table_ids)
        result["status"] = "success"

        print(f"✓ SUCCESS")
        print(f"  Records extracted: {result['records_extracted']}")
        print(f"  Tables found: {result['tables_found']}")
        print(f"  Sheets extracted: {len(sheet_names)}")
        print(f"  Sheet names: {', '.join(result['sheet_names_extracted'][:5])}")
        if len(result['sheet_names_extracted']) > 5:
            print(f"    ... and {len(result['sheet_names_extracted']) - 5} more")
        print(f"  Processing time: {result['processing_time_seconds']}s")

        # Check for potential issues
        if not sheet_names:
            result["warnings"].append("No sheet names extracted")
        if result["records_extracted"] == 0:
            result["warnings"].append("No records extracted")

        if result["warnings"]:
            print(f"  ⚠ Warnings: {', '.join(result['warnings'])}")

    except Exception as e:
        result["processing_time_seconds"] = round(time.time() - start_time, 2)
        result["status"] = "error"
        result["error"] = str(e)
        print(f"✗ ERROR: {result['error']}")
        print(f"  Processing time before error: {result['processing_time_seconds']}s")

    return result

def main():
    """Test all sample files."""
    # Read sample file paths
    with open("/home/hieutt50/projects/DSOL/sample_files_to_test.txt", 'r', encoding='utf-8') as f:
        sample_files = [line.strip() for line in f if line.strip()]

    print(f"Testing {len(sample_files)} sample files with V2 extraction pipeline\n")

    results = []
    for i, file_path in enumerate(sample_files, 1):
        print(f"\n[{i}/{len(sample_files)}]")
        result = test_file(file_path)
        results.append(result)

        # Small delay between files
        time.sleep(0.5)

    # Save results
    output_file = "/home/hieutt50/projects/DSOL/sample_files_test_results.json"
    with open(output_file, 'w', encoding='utf-8') as f:
        json.dump(results, f, indent=2, ensure_ascii=False)

    print(f"\n\n{'='*80}")
    print("TEST SUMMARY")
    print('='*80)

    total = len(results)
    success = sum(1 for r in results if r["status"] == "success")
    errors = sum(1 for r in results if r["status"] == "error")
    warnings = sum(1 for r in results if r["warnings"])

    print(f"Total files tested: {total}")
    print(f"Successful: {success} ({success/total*100:.1f}%)")
    print(f"Errors: {errors} ({errors/total*100:.1f}%)")
    print(f"With warnings: {warnings}")

    if success > 0:
        successful_results = [r for r in results if r["status"] == "success"]
        total_records = sum(r["records_extracted"] for r in successful_results)
        total_tables = sum(r["tables_found"] for r in successful_results)
        avg_time = sum(r["processing_time_seconds"] for r in successful_results) / len(successful_results)

        print(f"\nSuccessful extractions:")
        print(f"  Total records: {total_records}")
        print(f"  Total tables: {total_tables}")
        print(f"  Avg processing time: {avg_time:.2f}s")

    if errors > 0:
        print(f"\nErrors:")
        for r in results:
            if r["status"] == "error":
                print(f"  - {r['file_name']}: {r['error'][:100]}")

    print(f"\nDetailed results saved to: {output_file}")

if __name__ == "__main__":
    main()
