#!/usr/bin/env python3
"""
Test CLI integration with plugin system
"""

import sys
from pathlib import Path

# Add current directory to path for imports
sys.path.insert(0, str(Path(__file__).parent))

def test_plugin_cli_integration():
    """Test the CLI integration with plugin system."""

    # Import the command function
    from markitect.plugins.builtin.markdown_commands import md_render_command
    import click

    # Create a mock context
    class MockContext:
        def __init__(self):
            self.obj = {}
            self.resilient_parsing = False
            self.allow_extra_args = False
            self.allow_interspersed_args = True
            self.ignore_unknown_options = False
            self.help_option_names = ['--help']
            self.token_normalize_func = None
            self.color = None
            self.terminal_width = None
            self.max_content_width = None

    ctx = MockContext()

    # Test parameters
    input_file = "test_cli_plugin.md"
    output = "/tmp/test_cli_plugin_default.html"

    print("🧪 Testing CLI Plugin Integration")
    print("=" * 50)

    try:
        # Test 1: Default engine (should use testdrive-jsui for edit mode)
        print("\n1️⃣ Testing default engine for edit mode...")

        md_render_command(
            ctx=ctx,
            input_file=input_file,
            output=output,
            theme=None,
            css=None,
            edit=True,
            insert=False,
            engine=None,  # Should default to testdrive-jsui
            editor_theme='github',
            keyboard_shortcuts=True,
            use_publication_dir=False,
            dont_use_publication_dir=False,
            nodogtag=False,
            ship_assets=None,
            no_ship_assets=False,
            verbose=True,
            silent=False,
            image_max_width=None,
            image_max_height=None
        )

        print("✅ Default engine test completed")

        # Test 2: Explicit testdrive-jsui engine
        print("\n2️⃣ Testing explicit testdrive-jsui engine...")

        output2 = "/tmp/test_cli_plugin_explicit.html"
        md_render_command(
            ctx=ctx,
            input_file=input_file,
            output=output2,
            theme=None,
            css=None,
            edit=True,
            insert=False,
            engine='testdrive-jsui',  # Explicit engine
            editor_theme='github',
            keyboard_shortcuts=True,
            use_publication_dir=False,
            dont_use_publication_dir=False,
            nodogtag=False,
            ship_assets=None,
            no_ship_assets=False,
            verbose=True,
            silent=False,
            image_max_width=None,
            image_max_height=None
        )

        print("✅ Explicit engine test completed")

        # Test 3: Standard engine fallback
        print("\n3️⃣ Testing standard engine fallback...")

        output3 = "/tmp/test_cli_plugin_standard.html"
        md_render_command(
            ctx=ctx,
            input_file=input_file,
            output=output3,
            theme=None,
            css=None,
            edit=True,
            insert=False,
            engine='standard',  # Explicit standard engine
            editor_theme='github',
            keyboard_shortcuts=True,
            use_publication_dir=False,
            dont_use_publication_dir=False,
            nodogtag=False,
            ship_assets=None,
            no_ship_assets=False,
            verbose=True,
            silent=False,
            image_max_width=None,
            image_max_height=None
        )

        print("✅ Standard engine test completed")

        # Test 4: Unknown engine (should fallback)
        print("\n4️⃣ Testing unknown engine (should fallback to standard)...")

        output4 = "/tmp/test_cli_plugin_unknown.html"
        md_render_command(
            ctx=ctx,
            input_file=input_file,
            output=output4,
            theme=None,
            css=None,
            edit=True,
            insert=False,
            engine='unknown-engine',  # Unknown engine
            editor_theme='github',
            keyboard_shortcuts=True,
            use_publication_dir=False,
            dont_use_publication_dir=False,
            nodogtag=False,
            ship_assets=None,
            no_ship_assets=False,
            verbose=True,
            silent=False,
            image_max_width=None,
            image_max_height=None
        )

        print("✅ Unknown engine test completed")

        print("\n🎉 All CLI integration tests completed!")
        print("\nGenerated files:")
        for output_file in [output, output2, output3, output4]:
            if Path(output_file).exists():
                size = Path(output_file).stat().st_size
                print(f"  📄 {output_file} ({size:,} bytes)")

    except Exception as e:
        print(f"❌ CLI integration test failed: {e}")
        import traceback
        traceback.print_exc()
        return False

    return True

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