#!/usr/bin/env python3
"""Integration summary test - shows all components work together"""

import os
import sys
import tempfile

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))


def test_full_integration():
    """Test the full integration: CLI -> main.py -> model.py -> deepl module"""
    print("=" * 70)
    print("FULL INTEGRATION TEST: DeepL + LLM Translation Pipeline")
    print("=" * 70)

    # Step 1: Import all modules
    print("\n[Step 1] Importing modules...")
    try:
        from wenbi.llm.deepl import (
            configure_deepl,
            map_language_to_deepl_code,
            translate_with_deepl,
            is_deepl_available,
        )
        from wenbi.model import translate
        from wenbi.cli import handle_translate_command, add_global_args
        from wenbi.main import process_input
        print("✓ All modules imported successfully")
    except ImportError as e:
        print(f"✗ Import failed: {e}")
        return False

    # Step 2: Check deepl module structure
    print("\n[Step 2] Checking DeepL module structure...")
    deepl_funcs = [
        "configure_deepl",
        "map_language_to_deepl_code",
        "translate_with_deepl",
        "is_deepl_available",
    ]
    for func_name in deepl_funcs:
        try:
            from wenbi.llm import deepl as deepl_module
            func = getattr(deepl_module, func_name)
            print(f"  ✓ {func_name} exists")
        except AttributeError:
            print(f"  ✗ {func_name} missing")
            return False

    # Step 3: Check model.translate signature
    print("\n[Step 3] Checking translate function signature...")
    import inspect

    sig = inspect.signature(translate)
    params = list(sig.parameters.keys())
    print(f"  Parameters: {len(params)}")

    required_new_params = ["use_deepl", "deepl_key"]
    for param in required_new_params:
        if param in params:
            print(f"  ✓ {param} parameter added")
        else:
            print(f"  ✗ {param} parameter missing")
            return False

    # Step 4: Check CLI integration
    print("\n[Step 4] Checking CLI integration...")
    import argparse

    parser = argparse.ArgumentParser()
    subparser = parser.add_subparsers(dest="command").add_parser("translate")
    add_global_args(subparser)

    # Parse with DeepL options
    args = subparser.parse_args(
        ["input.txt", "--no-deepl", "--deepl-key", "test-123"]
    )

    cli_checks = [
        ("input", "input.txt"),
        ("no_deepl", True),
        ("deepl_key", "test-123"),
    ]

    for attr_name, expected in cli_checks:
        if hasattr(args, attr_name):
            actual = getattr(args, attr_name)
            if actual == expected:
                print(f"  ✓ {attr_name} = {actual}")
            else:
                print(f"  ✗ {attr_name} = {actual} (expected {expected})")
        else:
            print(f"  ✗ {attr_name} not found")
            return False

    # Step 5: Test language mapping
    print("\n[Step 5] Testing language mapping...")
    from wenbi.llm.deepl import map_language_to_deepl_code

    test_langs = [
        ("English", "EN"),
        ("French", "FR"),
        ("Chinese", "ZH"),
        ("German", "DE"),
    ]

    for lang, expected_code in test_langs:
        code = map_language_to_deepl_code(lang)
        if code == expected_code:
            print(f"  ✓ {lang} -> {code}")
        else:
            print(f"  ✗ {lang} -> {code} (expected {expected_code})")
            return False

    # Step 6: Test DeepL availability detection
    print("\n[Step 6] Testing DeepL availability detection...")
    from wenbi.llm.deepl import is_deepl_available

    # Should be False without key
    os.environ.pop("DEEPL_API_KEY", None)
    available = is_deepl_available()
    print(f"  DeepL available (no key): {available}")

    # Step 7: Create test VTT and run translation
    print("\n[Step 7] Running end-to-end translation test...")
    test_vtt = """WEBVTT

00:00:00.000 --> 00:00:05.000
Hello world

00:00:05.000 --> 00:00:10.000
This is a test
"""

    with tempfile.NamedTemporaryFile(
        mode="w", suffix=".vtt", delete=False, encoding="utf-8"
    ) as f:
        f.write(test_vtt)
        test_vtt_path = f.name

    try:
        with tempfile.TemporaryDirectory() as tmpdir:
            # Run translate with use_deepl=False (force LLM only)
            result = translate(
                test_vtt_path,
                output_dir=tmpdir,
                translate_language="French",
                llm="ollama/qwen3",
                chunk_length=10,
                use_deepl=False,  # Disable DeepL fallback
                verbose=False,
            )

            if result and len(result) > 0:
                print(f"  ✓ Translation completed")
                print(f"    Input: {len(test_vtt)} chars")
                print(f"    Output: {len(result)} chars")
            else:
                print(f"  ✗ Translation failed")
                return False

    except Exception as e:
        print(f"  ✗ Translation error: {e}")
        import traceback

        traceback.print_exc()
        return False
    finally:
        if os.path.exists(test_vtt_path):
            os.unlink(test_vtt_path)

    # Summary
    print("\n" + "=" * 70)
    print("INTEGRATION TEST SUMMARY")
    print("=" * 70)
    print("\n✓ DeepL module structure: VALID")
    print("✓ Model.translate signature: UPDATED")
    print("✓ CLI integration: WORKING")
    print("✓ Language mapping: FUNCTIONAL")
    print("✓ Availability detection: FUNCTIONAL")
    print("✓ End-to-end translation: WORKING")

    print("\n" + "=" * 70)
    print("🎉 FULL INTEGRATION TEST PASSED")
    print("=" * 70)
    print("\nDeepL integration is ready:")
    print("  • Primary translator: DeepL API")
    print("  • Fallback translator: LLM (dspy)")
    print("  • Configuration: Via --deepl-key or DEEPL_API_KEY env var")
    print("  • Disable DeepL: Use --no-deepl flag")
    print("\nExample usage:")
    print("  wenbi translate input.txt --lang French --deepl-key YOUR_KEY")
    print("  wenbi tr video.mp4 --lang Chinese --no-deepl  # LLM only")

    return True


if __name__ == "__main__":
    success = test_full_integration()
    sys.exit(0 if success else 1)
