"""
Select representative sample files for testing.
"""
import json

# Load analysis results
with open("/home/hieutt50/projects/DSOL/customer_files_analysis.json", 'r', encoding='utf-8') as f:
    results = json.load(f)

# Filter successful files
success_files = [r for r in results if r["status"] == "success"]
error_files = [r for r in results if r["status"] == "error"]

print("=== SUCCESSFUL FILES ===")
print(f"Total: {len(success_files)}\n")

# Categorize
small_files = [f for f in success_files if f["file_size_mb"] < 0.1]
medium_files = [f for f in success_files if 0.1 <= f["file_size_mb"] < 1.0]
large_files = [f for f in success_files if f["file_size_mb"] >= 1.0]
inflated_files = [f for f in success_files if f["has_inflated_sheets"]]
multi_sheet = [f for f in success_files if f["total_sheets"] > 5]

print(f"Small files (<0.1MB): {len(small_files)}")
print(f"Medium files (0.1-1MB): {len(medium_files)}")
print(f"Large files (>=1MB): {len(large_files)}")
print(f"Inflated sheets: {len(inflated_files)}")
print(f"Multi-sheet (>5): {len(multi_sheet)}")

# Select representative sample
sample_files = []

# 1. One large file with many sheets
if large_files:
    sample = max(large_files, key=lambda x: x["file_size_mb"])
    sample_files.append(("large_multi_sheet", sample))

# 2. One inflated file
if inflated_files:
    sample_files.append(("inflated", inflated_files[0]))

# 3. One medium .xlsm with multiple sheets
medium_xlsm = [f for f in medium_files if f["extension"] == ".xlsm" and f["total_sheets"] > 3]
if medium_xlsm:
    sample_files.append(("medium_xlsm", medium_xlsm[0]))

# 4. One small .xlsx
if small_files:
    small_xlsx = [f for f in small_files if f["extension"] == ".xlsx"]
    if small_xlsx:
        sample_files.append(("small_xlsx", small_xlsx[0]))

# 5. One medium .xlsx
if medium_files:
    medium_xlsx = [f for f in medium_files if f["extension"] == ".xlsx"]
    if medium_xlsx:
        sample_files.append(("medium_xlsx", medium_xlsx[0]))

# 6. One file with many merged cells
high_merge = [f for f in success_files if any(s.get("merged_cells_count", 0) > 10 for s in f["sheets"])]
if high_merge:
    sample_files.append(("high_merge", high_merge[0]))

# 7. One file with images
image_files = [f for f in success_files if f["has_images"]]
if image_files:
    sample_files.append(("with_images", image_files[0]))

print("\n=== SELECTED SAMPLE FILES FOR TESTING ===")
for i, (category, file_info) in enumerate(sample_files, 1):
    print(f"\n{i}. {category.upper()}")
    print(f"   File: {file_info['file_name']}")
    print(f"   Path: {file_info['file_path']}")
    print(f"   Size: {file_info['file_size_mb']}MB")
    print(f"   Extension: {file_info['extension']}")
    print(f"   Sheets: {file_info['total_sheets']}")
    print(f"   Inflated: {file_info['has_inflated_sheets']}")
    print(f"   Merged cells: {file_info['has_merged_cells']}")
    print(f"   Images: {file_info['has_images']}")
    print(f"   Load time: {file_info['load_time_seconds']}s")

print("\n=== ERROR FILES ===")
print(f"Total: {len(error_files)}")
xls_errors = [f for f in error_files if f["extension"] == ".xls"]
print(f".xls files (old format): {len(xls_errors)}")
other_errors = [f for f in error_files if f["extension"] != ".xls"]
if other_errors:
    print(f"Other errors: {len(other_errors)}")
    for err in other_errors:
        print(f"  - {err['file_name']}: {err['error']}")

# Save sample files list
sample_paths = [f[1]["file_path"] for f in sample_files]
with open("/home/hieutt50/projects/DSOL/sample_files_to_test.txt", 'w', encoding='utf-8') as f:
    for path in sample_paths:
        f.write(path + "\n")

print(f"\n\nSample file paths saved to: /home/hieutt50/projects/DSOL/sample_files_to_test.txt")
