"""Test script to import and validate all SQLAlchemy models."""
import sys
import traceback
import os

# Set UTF-8 encoding for Windows console
if os.name == 'nt':
    import io
    sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')

def test_model_import():
    """Test importing all models from api.models."""
    try:
        import api.models
        print("[SUCCESS] Import successful")

        # Get all model classes (exclude private attributes and modules)
        all_classes = [attr for attr in dir(api.models) if not attr.startswith('_') and attr[0].isupper()]

        # Filter out base classes and mixins (they don't have __tablename__)
        base_classes = {'Base', 'TimestampMixin', 'UUIDMixin'}
        models = [m for m in all_classes if m not in base_classes]

        print(f"\nTotal classes found: {len(all_classes)}")
        print(f"Base classes/mixins: {len(base_classes)}")
        print(f"Table models: {len(models)}")
        print("\nTable Models:")
        for m in sorted(models):
            print(f"  - {m}")

        return models
    except Exception as e:
        print(f"[ERROR] Import failed: {e}")
        traceback.print_exc()
        return []

def test_model_structure(model_name):
    """Test individual model structure and configuration."""
    import api.models

    try:
        model_cls = getattr(api.models, model_name)

        # Check if it's actually a class
        if not isinstance(model_cls, type):
            return f"[FAIL] {model_name} - Not a class"

        # Check for __tablename__
        if not hasattr(model_cls, '__tablename__'):
            return f"[FAIL] {model_name} - Missing __tablename__"

        # Check for __table_args__ (optional but should exist if defined)
        has_table_args = hasattr(model_cls, '__table_args__')

        # Try to instantiate (without saving to DB)
        try:
            instance = model_cls()
            can_instantiate = True
        except Exception as inst_error:
            can_instantiate = False
            inst_msg = str(inst_error)

        # Build result message
        details = []
        details.append(f"table={model_cls.__tablename__}")
        if has_table_args:
            details.append("has_table_args")
        if can_instantiate:
            details.append("instantiable")
        else:
            details.append(f"not_instantiable({inst_msg[:50]})")

        return f"[PASS] {model_name} - {', '.join(details)}"

    except Exception as e:
        return f"[FAIL] {model_name} - {str(e)}"

def main():
    print("=" * 70)
    print("ClaudeTools - Model Import and Structure Test")
    print("=" * 70)

    # Test 1: Import all models
    print("\n[TEST 1] Importing api.models module...")
    models = test_model_import()

    if not models:
        print("\n[CRITICAL] Failed to import models module")
        sys.exit(1)

    # Test 2: Validate each model structure
    print(f"\n[TEST 2] Validating structure of {len(models)} models...")
    print("-" * 70)

    passed = 0
    failed = 0
    results = []

    for model_name in sorted(models):
        result = test_model_structure(model_name)
        results.append(result)

        if result.startswith("[PASS]"):
            passed += 1
        else:
            failed += 1

    # Print all results
    for result in results:
        print(result)

    # Summary
    print("-" * 70)
    print(f"\n[SUMMARY]")
    print(f"Total models: {len(models)}")
    print(f"[PASS] Passed: {passed}")
    print(f"[FAIL] Failed: {failed}")

    if failed == 0:
        print(f"\n[SUCCESS] All {passed} models validated successfully!")
        sys.exit(0)
    else:
        print(f"\n[WARNING] {failed} model(s) need attention")
        sys.exit(1)

if __name__ == "__main__":
    main()
