"""Python language handler implementation.

Thin delegation layer that routes requests to the actual module implementations.
"""

from __future__ import annotations

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from aiservice.llm_models import LLM
    from authapp.auth import AuthenticatedRequest
    from core.languages.python.code_repair.code_repair import (
        CodeRepairErrorResponseSchema,
        CodeRepairIntermediateResponseItemschema,
    )
    from core.languages.python.code_repair.code_repair_context import CodeRepairContext
    from core.languages.python.explanations.models import (
        ExplanationsErrorResponseSchema,
        ExplanationsResponseSchema,
        ExplanationsSchema,
    )
    from core.languages.python.optimization_review.optimization_review import (
        OptimizationReviewErrorSchema,
        OptimizationReviewResponseSchema,
        OptimizationReviewSchema,
    )
    from core.languages.python.optimizer.models import JitRewriteOptimizeSchema
    from core.shared.optimizer_models import OptimizeSchema
    from core.shared.optimizer_schemas import OptimizeErrorResponseSchema, OptimizeResponseSchema
    from core.shared.testgen_models import TestGenErrorResponseSchema, TestGenResponseSchema, TestGenSchema


class PythonHandler:
    """Python language handler."""

    language = "python"

    supports_testgen = True
    supports_optimizer = True
    supports_code_repair = True
    supports_jit_rewrite = True
    supports_optimization_review = True
    supports_explanations = True

    async def testgen_generate(
        self, request: AuthenticatedRequest, data: TestGenSchema
    ) -> tuple[int, TestGenResponseSchema | TestGenErrorResponseSchema]:
        """Generate tests for Python code."""
        from core.languages.python.testgen.generate import testgen_python  # noqa: PLC0415

        return await testgen_python(request, data)

    async def optimizer_optimize(
        self, request: AuthenticatedRequest, data: OptimizeSchema
    ) -> tuple[int, OptimizeResponseSchema | OptimizeErrorResponseSchema]:
        """Optimize Python code for performance."""
        from core.languages.python.optimizer.optimizer import optimize_python  # noqa: PLC0415

        return await optimize_python(request, data)

    async def code_repair_repair(
        self, user_id: str, optimization_id: str, ctx: CodeRepairContext, optimize_model: LLM | None = None
    ) -> CodeRepairIntermediateResponseItemschema | CodeRepairErrorResponseSchema:
        """Repair Python code based on error messages."""
        from core.languages.python.code_repair.code_repair import code_repair  # noqa: PLC0415

        kwargs = {}
        if optimize_model is not None:
            kwargs["optimize_model"] = optimize_model
        return await code_repair(user_id, optimization_id, ctx, **kwargs)

    async def jit_rewrite(
        self, request: AuthenticatedRequest, data: JitRewriteOptimizeSchema
    ) -> tuple[int, OptimizeResponseSchema | OptimizeErrorResponseSchema]:
        """Perform JIT rewriting of Python code."""
        from core.languages.python.jit_rewrite.jit_rewrite import jit_rewrite  # noqa: PLC0415

        return await jit_rewrite(request, data)

    async def optimization_review(
        self,
        request: AuthenticatedRequest,
        data: OptimizationReviewSchema,
        optimization_review_model: LLM | None = None,
    ) -> tuple[int, OptimizationReviewResponseSchema | OptimizationReviewErrorSchema]:
        """Review Python code for optimization opportunities."""
        from core.languages.python.optimization_review.optimization_review import get_optimization_review  # noqa: PLC0415

        kwargs = {}
        if optimization_review_model is not None:
            kwargs["optimization_review_model"] = optimization_review_model
        return await get_optimization_review(request, data, **kwargs)

    async def explain_optimizations(
        self, user_id: str, data: ExplanationsSchema, explanations_model: LLM | None = None
    ) -> ExplanationsResponseSchema | ExplanationsErrorResponseSchema:
        """Explain optimizations made to Python code."""
        from core.languages.python.explanations.explanations import explain_optimizations  # noqa: PLC0415

        kwargs = {}
        if explanations_model is not None:
            kwargs["explanations_model"] = explanations_model
        return await explain_optimizations(user_id, data, **kwargs)
