"""Test baseline creation with zip_baselines setting."""
import os
import shutil
import tempfile
from pathlib import Path

# Create test project
temp_project = tempfile.mkdtemp(prefix="test_zip_setting_")

try:
    # Create test files
    (Path(temp_project) / "file1.py").write_text("# File 1\nprint('test')\n", encoding='utf-8')
    (Path(temp_project) / "file2.py").write_text("# File 2\ndef func():\n    pass\n", encoding='utf-8')
    
    print(f"Test project: {temp_project}")
    print()
    
    from pyucc.core.differ import BaselineManager
    from pyucc.config import settings as app_settings
    
    baseline_dir = tempfile.mkdtemp(prefix="test_baselines_")
    bm = BaselineManager(temp_project, baselines_root=baseline_dir)
    
    # Test 1: With zip_baselines = False (default)
    print("=" * 60)
    print("Test 1: zip_baselines = False (default)")
    print("=" * 60)
    app_settings.save_settings({"zip_baselines": False})
    print(f"Setting zip_baselines = {app_settings.get_zip_baselines()}")
    
    baseline_id_1 = bm.create_baseline_from_dir(
        temp_project,
        baseline_id="test_no_zip",
        snapshot=True,
        compute_sha1=False
    )
    
    baseline_path_1 = bm._baseline_dir(baseline_id_1)
    files_dir_1 = os.path.join(baseline_path_1, "files")
    files_zip_1 = os.path.join(baseline_path_1, "files.zip")
    
    print(f"Created baseline: {baseline_id_1}")
    print(f"  files/ exists: {os.path.exists(files_dir_1)}")
    print(f"  files.zip exists: {os.path.exists(files_zip_1)}")
    
    if os.path.exists(files_dir_1):
        file_count = len(list(Path(files_dir_1).glob("*.py")))
        print(f"  Files in directory: {file_count}")
    
    result_1 = "✅ PASS" if (os.path.exists(files_dir_1) and not os.path.exists(files_zip_1)) else "❌ FAIL"
    print(f"Result: {result_1} - Directory copy without zip")
    print()
    
    # Test 2: With zip_baselines = True
    print("=" * 60)
    print("Test 2: zip_baselines = True")
    print("=" * 60)
    app_settings.save_settings({"zip_baselines": True})
    print(f"Setting zip_baselines = {app_settings.get_zip_baselines()}")
    
    baseline_id_2 = bm.create_baseline_from_dir(
        temp_project,
        baseline_id="test_with_zip",
        snapshot=True,
        compute_sha1=False
    )
    
    baseline_path_2 = bm._baseline_dir(baseline_id_2)
    files_dir_2 = os.path.join(baseline_path_2, "files")
    files_zip_2 = os.path.join(baseline_path_2, "files.zip")
    
    print(f"Created baseline: {baseline_id_2}")
    print(f"  files/ exists: {os.path.exists(files_dir_2)}")
    print(f"  files.zip exists: {os.path.exists(files_zip_2)}")
    
    if os.path.exists(files_dir_2):
        file_count = len(list(Path(files_dir_2).glob("*.py")))
        print(f"  Files in directory: {file_count}")
    
    if os.path.exists(files_zip_2):
        zip_size = os.path.getsize(files_zip_2)
        print(f"  Zip size: {zip_size} bytes")
    
    result_2 = "✅ PASS" if (os.path.exists(files_dir_2) and os.path.exists(files_zip_2)) else "❌ FAIL"
    print(f"Result: {result_2} - Directory copy WITH zip archive")
    print()
    
    # Summary
    print("=" * 60)
    print("SUMMARY")
    print("=" * 60)
    print(f"Test 1 (no zip): {result_1}")
    print(f"Test 2 (with zip): {result_2}")
    print()
    
    if result_1 == "✅ PASS" and result_2 == "✅ PASS":
        print("🎉 ALL TESTS PASSED!")
        print()
        print("Settings behavior:")
        print("  • zip_baselines = False → files/ only (fast, default)")
        print("  • zip_baselines = True → files/ + files.zip (saves space)")
    else:
        print("⚠️  SOME TESTS FAILED")

finally:
    # Cleanup
    shutil.rmtree(temp_project, ignore_errors=True)
    if 'baseline_dir' in locals():
        shutil.rmtree(baseline_dir, ignore_errors=True)
