#!/usr/bin/env python3
"""
Auto-update the imports in _imports.py.

This script generates all imports by parsing the installed pipecat-ai package
and completely overwrites _imports.py with the new content.

Usage:
    uv run scripts/cli/imports/update_imports.py           # Update _imports.py
    uv run scripts/cli/imports/update_imports.py --preview # Preview without updating

Most callers should use the top-level ``scripts/cli/update_registry.py``, which
regenerates both _imports.py and _configs.py.
"""

import argparse
import sys
from pathlib import Path

# Add src and scripts/cli/imports to path
project_root = Path(__file__).parent.parent.parent.parent
sys.path.insert(0, str(project_root / "src"))
sys.path.insert(0, str(project_root / "scripts" / "cli" / "imports"))

from import_generator import (
    find_pipecat_install_path,
    format_feature_imports,
    format_imports_dict,
    generate_imports_dict,
)

from pipecat.cli.registry import ServiceRegistry


def preview_imports():
    """Generate and print imports for all services and features."""
    print("# Auto-generated imports for Pipecat services and features")
    print("# Generated by scripts/cli/imports/update_imports.py --preview")
    print("# Source: Parsed from installed pipecat-ai package using AST")
    print()

    # Find pipecat installation
    pipecat_path = find_pipecat_install_path()
    if not pipecat_path:
        print("# ERROR: pipecat-ai not installed. Run: uv sync", file=sys.stderr)
        return False

    # Generate all imports (including transports)
    imports_dict = generate_imports_dict()

    # Print transport imports
    print("    # Transports - WebRTC")
    for transport in ServiceRegistry.WEBRTC_TRANSPORTS:
        transport_value = transport.value
        if transport_value in imports_dict:
            import_stmt = imports_dict[transport_value][0]
            print(f'    "{transport_value}": ["{import_stmt}"],')
    print()

    print("    # Transports - Telephony")
    for transport in ServiceRegistry.TELEPHONY_TRANSPORTS:
        transport_value = transport.value
        if transport_value in imports_dict:
            import_stmt = imports_dict[transport_value][0]
            print(f'    "{transport_value}": ["{import_stmt}"],')
    print()

    # Print STT imports
    print("    # STT Services")
    for service in ServiceRegistry.STT_SERVICES:
        service_value = service.value
        if service_value in imports_dict:
            import_stmt = imports_dict[service_value][0]
            print(f'    "{service_value}": ["{import_stmt}"],')
        else:
            print(f'    # TODO: "{service_value}": [...],  # Could not discover import')
    print()

    # Print LLM imports
    print("    # LLM Services")
    for service in ServiceRegistry.LLM_SERVICES:
        service_value = service.value
        if service_value in imports_dict:
            import_stmt = imports_dict[service_value][0]
            print(f'    "{service_value}": ["{import_stmt}"],')
        else:
            print(f'    # TODO: "{service_value}": [...],  # Could not discover import')
    print()

    # Print TTS imports
    print("    # TTS Services")
    for service in ServiceRegistry.TTS_SERVICES:
        service_value = service.value
        if service_value in imports_dict:
            import_stmt = imports_dict[service_value][0]
            print(f'    "{service_value}": ["{import_stmt}"],')
        else:
            print(f'    # TODO: "{service_value}": [...],  # Could not discover import')
    print()

    # Print Realtime imports
    print("    # Realtime Services")
    for service in ServiceRegistry.REALTIME_SERVICES:
        service_value = service.value
        if service_value in imports_dict:
            import_stmt = imports_dict[service_value][0]
            print(f'    "{service_value}": ["{import_stmt}"],')
        else:
            print(f'    # TODO: "{service_value}": [...],  # Could not discover import')
    print()

    # Print Video imports
    print("    # Video Services")
    for service in ServiceRegistry.VIDEO_SERVICES:
        service_value = service.value
        if service_value in imports_dict:
            import_stmt = imports_dict[service_value][0]
            print(f'    "{service_value}": ["{import_stmt}"],')
        else:
            print(f'    # TODO: "{service_value}": [...],  # Could not discover import')
    print()

    # Print feature imports
    print("    # Feature Imports")
    for line in format_feature_imports(pipecat_path):
        print(line)

    return True


def update_services_imports_file():
    """Generate and write the complete _imports.py file."""
    services_imports_file = (
        Path(__file__).parent.parent.parent.parent
        / "src"
        / "pipecat"
        / "cli"
        / "registry"
        / "_imports.py"
    )

    print("📝 Generating imports...")

    # Find pipecat installation
    pipecat_path = find_pipecat_install_path()
    if not pipecat_path:
        print("❌ Could not find pipecat-ai installation")
        return

    # Generate all imports
    imports_dict = generate_imports_dict()

    # Format as complete Python module (this has 4-space indentation for class context)
    imports_code = format_imports_dict(imports_dict, pipecat_path)

    # Remove the 4-space indentation since we're writing to a module-level file
    imports_code_lines = imports_code.split("\n")
    unindented_lines = [
        line[4:] if line.startswith("    ") else line for line in imports_code_lines
    ]
    imports_code = "\n".join(unindented_lines)

    # Add BASE_IMPORTS at the end
    base_imports = """

# Base imports always included in generated bot files
BASE_IMPORTS = [
    "import os",
    "from loguru import logger",
]
"""

    # Create the complete file content
    header = '''"""AUTO-GENERATED SERVICE IMPORTS.

⚠️  DO NOT EDIT THIS FILE DIRECTLY ⚠️

This file is automatically generated from service_metadata.py.
To make changes, edit service_metadata.py and run:
  uv run scripts/cli/update_registry.py

Source: scripts/cli/imports/import_generator.py
"""

# Import statements mapping for services and transports
'''

    complete_content = header + imports_code + base_imports

    # Write the complete file
    print(f"✍️  Writing {services_imports_file}")
    with open(services_imports_file, "w") as f:
        f.write(complete_content)

    print("✅ Successfully updated _imports.py")

    # Format with ruff
    print("🔍 Formatting with ruff...")
    import subprocess

    result = subprocess.run(
        ["ruff", "format", str(services_imports_file)],
        capture_output=True,
        text=True,
    )
    if result.returncode == 0:
        print("✅ File formatted successfully")
    else:
        print(f"⚠️  Formatting warning: {result.stderr}")


def main():
    """Main entry point with argument parsing."""
    parser = argparse.ArgumentParser(description="Generate or preview Pipecat service imports")
    parser.add_argument(
        "--preview",
        action="store_true",
        help="Preview imports without updating _imports.py",
    )
    args = parser.parse_args()

    if args.preview:
        success = preview_imports()
        sys.exit(0 if success else 1)
    else:
        update_services_imports_file()


if __name__ == "__main__":
    main()
