#!/usr/bin/env python3
"""
Script to generate .pyi stub files for the synchronous API wrappers.
This allows generating stubs without running the full Studio application.
"""

import os
import sys
import logging
import importlib

# Add Studio to path so we can import modules
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from studio_api.internal.async_to_sync import AsyncToSyncConverter
from studio_api.version_list import supported_versions


def generate_stubs_for_module(module_name: str) -> None:
    """Generate stub files for a specific module that exports StudioAPI and StudioAPISync."""
    try:
        # Import the module
        module = importlib.import_module(module_name)

        # Check if module has StudioAPISync (the sync wrapper)
        if hasattr(module, "StudioAPISync"):
            # Module already has a sync class
            api_class = getattr(module, "StudioAPI", None)
            sync_class = getattr(module, "StudioAPISync")

            if api_class:
                # Generate the stub file
                AsyncToSyncConverter.generate_stub_file(api_class, sync_class)
                logging.info(f"Generated stub file for {module_name}")
            else:
                logging.warning(
                    f"Module {module_name} has StudioAPISync but no StudioAPI"
                )

        elif hasattr(module, "StudioAPI"):
            # Module only has async API, need to create sync wrapper first
            from studio_api.internal.async_to_sync import create_sync_class

            api_class = getattr(module, "StudioAPI")
            sync_class = create_sync_class(api_class)

            # Generate the stub file
            AsyncToSyncConverter.generate_stub_file(api_class, sync_class)
            logging.info(f"Generated stub file for {module_name}")
        else:
            logging.warning(
                f"Module {module_name} does not export StudioAPI or StudioAPISync"
            )

    except Exception as e:
        logging.error(f"Failed to generate stub for {module_name}: {e}")
        import traceback

        traceback.print_exc()


def main():
    """Main function to generate all API stub files."""
    logging.basicConfig(level=logging.INFO)

    logging.info("Starting stub generation...")

    # Dynamically get module names from supported_versions
    api_modules = []
    for api_class in supported_versions:
        # Extract module name from the class
        module_name = api_class.__module__
        if module_name not in api_modules:
            api_modules.append(module_name)

    logging.info(f"Found {len(api_modules)} API modules: {api_modules}")

    # Generate stubs for each module
    for module_name in api_modules:
        generate_stubs_for_module(module_name)

    logging.info("Stub generation complete!")


if __name__ == "__main__":
    main()
