#!/usr/bin/env python3
"""
Validation script for distance threshold feature.
Checks configuration and code changes without loading full app.
"""

import sys
import re
from pathlib import Path


def check_config_file():
    """Verify config.py has distance threshold settings."""
    print("1. Checking app/config.py...")
    config_path = Path("app/config.py")
    content = config_path.read_text()
    
    checks = [
        ("EMBEDDING_TOP_K", "EMBEDDING_TOP_K" in content),
        ("EMBEDDING_DISTANCE_THRESHOLD", "EMBEDDING_DISTANCE_THRESHOLD" in content),
    ]
    
    all_passed = True
    for check_name, result in checks:
        status = "✓" if result else "✗"
        print(f"   {status} {check_name}")
        all_passed = all_passed and result
    
    return all_passed


def check_query_service():
    """Verify QueryService has distance threshold support."""
    print("2. Checking app/services/query_service.py...")
    service_path = Path("app/services/query_service.py")
    content = service_path.read_text()
    
    checks = [
        ("similarity_search_with_score", "similarity_search_with_score" in content),
        ("distance_threshold parameter", "distance_threshold" in content),
        ("distance filtering logic", "distance > distance_threshold" in content),
    ]
    
    all_passed = True
    for check_name, result in checks:
        status = "✓" if result else "✗"
        print(f"   {status} {check_name}")
        all_passed = all_passed and result
    
    return all_passed


def check_main_api():
    """Verify main.py has distance threshold support."""
    print("3. Checking app/main.py...")
    main_path = Path("app/main.py")
    content = main_path.read_text()
    
    checks = [
        ("QueryRequest with distance_threshold", "distance_threshold: Optional" in content),
        ("DocumentResponse with distance field", "distance: Optional[float]" in content),
        ("Query endpoint uses threshold", "distance_threshold" in content and "query_service.search" in content),
        ("Config defaults applied", "settings.EMBEDDING_TOP_K" in content and "settings.EMBEDDING_DISTANCE_THRESHOLD" in content),
    ]
    
    all_passed = True
    for check_name, result in checks:
        status = "✓" if result else "✗"
        print(f"   {status} {check_name}")
        all_passed = all_passed and result
    
    return all_passed


def check_env_example():
    """Verify .env.example has distance threshold settings."""
    print("4. Checking .env.example...")
    env_path = Path(".env.example")
    content = env_path.read_text()
    
    checks = [
        ("EMBEDDING_TOP_K", "EMBEDDING_TOP_K" in content),
        ("EMBEDDING_DISTANCE_THRESHOLD", "EMBEDDING_DISTANCE_THRESHOLD" in content),
    ]
    
    all_passed = True
    for check_name, result in checks:
        status = "✓" if result else "✗"
        print(f"   {status} {check_name}")
        all_passed = all_passed and result
    
    return all_passed


def check_test_files():
    """Verify test files exist."""
    print("5. Checking test files...")
    test_files = [
        "tests/test_distance_threshold.py",
        "tests/test_distance_threshold_integration.py",
    ]
    
    all_passed = True
    for test_file in test_files:
        path = Path(test_file)
        exists = path.exists()
        status = "✓" if exists else "✗"
        print(f"   {status} {test_file}")
        all_passed = all_passed and exists
    
    return all_passed


def check_script_files():
    """Verify script files exist."""
    print("6. Checking script files...")
    script_files = [
        "scripts/evaluate_distance_threshold.py",
        "scripts/test_distance_threshold.sh",
    ]
    
    all_passed = True
    for script_file in script_files:
        path = Path(script_file)
        exists = path.exists()
        status = "✓" if exists else "✗"
        print(f"   {status} {script_file}")
        all_passed = all_passed and exists
    
    return all_passed


def check_documentation():
    """Verify documentation exists."""
    print("7. Checking documentation...")
    doc_files = [
        "DISTANCE_THRESHOLD_GUIDE.md",
    ]
    
    all_passed = True
    for doc_file in doc_files:
        path = Path(doc_file)
        exists = path.exists()
        status = "✓" if exists else "✗"
        print(f"   {status} {doc_file}")
        all_passed = all_passed and exists
    
    return all_passed


def check_code_quality():
    """Check code quality (no syntax errors)."""
    print("8. Checking code syntax...")
    
    import ast
    
    files_to_check = [
        "app/config.py",
        "app/services/query_service.py",
        "app/main.py",
    ]
    
    all_passed = True
    for file_path in files_to_check:
        try:
            path = Path(file_path)
            content = path.read_text()
            ast.parse(content)
            print(f"   ✓ {file_path}")
        except SyntaxError as e:
            print(f"   ✗ {file_path}: {e}")
            all_passed = False
    
    return all_passed


def main():
    """Run all validation checks."""
    print("=" * 70)
    print("Distance Threshold Feature Validation")
    print("=" * 70)
    print()
    
    results = []
    results.append(("Config file", check_config_file()))
    print()
    results.append(("QueryService", check_query_service()))
    print()
    results.append(("Main API", check_main_api()))
    print()
    results.append(("Environment file", check_env_example()))
    print()
    results.append(("Test files", check_test_files()))
    print()
    results.append(("Script files", check_script_files()))
    print()
    results.append(("Documentation", check_documentation()))
    print()
    results.append(("Code quality", check_code_quality()))
    
    print()
    print("=" * 70)
    print("Summary")
    print("=" * 70)
    
    all_passed = True
    for check_name, passed in results:
        status = "✓" if passed else "✗"
        print(f"{status} {check_name}")
        all_passed = all_passed and passed
    
    print("=" * 70)
    
    if all_passed:
        print("\n✓ All validation checks passed!")
        print("\nNext steps:")
        print("1. Review DISTANCE_THRESHOLD_GUIDE.md")
        print("2. Start Redis: docker compose up -d redis")
        print("3. Run tests: pytest tests/test_distance_threshold*.py -v")
        print("4. Start API: uvicorn app.main:app --reload")
        print("5. Ingest data: python app/ingest.py")
        print("6. Run evaluation: python scripts/evaluate_distance_threshold.py")
        return 0
    else:
        print("\n✗ Some validation checks failed")
        return 1


if __name__ == "__main__":
    sys.exit(main())
