"""Quick test to verify directory copy is working instead of zip."""
import os
import shutil
import tempfile
from pathlib import Path

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

try:
    # Create just 2 files
    (Path(temp_project) / "file1.py").write_text("# File 1\nprint('hello')\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}")
    
    from pyucc.core.differ import BaselineManager
    
    baseline_dir = tempfile.mkdtemp(prefix="quick_baseline_")
    bm = BaselineManager(temp_project, baselines_root=baseline_dir)
    
    print("Creating baseline (without countings/metrics)...")
    
    # Manually create a simple baseline to test just the snapshot mechanism
    baseline_id = "quick_test_baseline"
    from pyucc.core.differ import FileMeta, BaselineMetadata
    import time
    import json
    
    dest = bm._baseline_dir(baseline_id)
    os.makedirs(dest, exist_ok=True)
    
    # Create minimal metadata
    files_meta = []
    for f in Path(temp_project).glob("*.py"):
        rel_path = f.name
        files_meta.append(FileMeta(
            path=rel_path,
            size=f.stat().st_size,
            mtime=f.stat().st_mtime,
            sha1=None,
            countings=None,
            metrics=None
        ))
    
    metadata = BaselineMetadata(
        baseline_id=baseline_id,
        created_at=time.time(),
        source="local",
        origin=None,
        project_root=temp_project,
        files=files_meta,
        profile=None
    )
    
    # Save metadata
    meta_path = bm.get_metadata_path(baseline_id)
    with open(meta_path, "w", encoding="utf-8") as f:
        json.dump(bm._metadata_to_dict(metadata), f, indent=2)
    
    # Test the snapshot creation (directory copy)
    print("Creating snapshot using directory copy...")
    snapshot_dir = os.path.join(dest, "files")
    os.makedirs(snapshot_dir, exist_ok=True)
    
    for fm in files_meta:
        src_file = os.path.join(temp_project, fm.path)
        dst_file = os.path.join(snapshot_dir, fm.path)
        dst_parent = os.path.dirname(dst_file)
        if dst_parent:
            os.makedirs(dst_parent, exist_ok=True)
        shutil.copy2(src_file, dst_file)
    
    # Check results
    print("\nResults:")
    files_dir = os.path.join(dest, "files")
    files_zip = os.path.join(dest, "files.zip")
    
    print(f"  files/ exists: {os.path.exists(files_dir)}")
    print(f"  files.zip exists: {os.path.exists(files_zip)}")
    
    if os.path.exists(files_dir):
        copied_files = list(Path(files_dir).glob("*.py"))
        print(f"  Copied files: {len(copied_files)}")
        for f in copied_files:
            print(f"    - {f.name}")
    
    print()
    if os.path.exists(files_dir) and not os.path.exists(files_zip):
        print("✅ SUCCESS: Directory copy working correctly!")
        print("   No zip file created (faster and simpler)")
    elif os.path.exists(files_zip):
        print("❌ FAIL: files.zip still created")
    else:
        print("❌ FAIL: No snapshot created")

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