"""Python language handler implementation.

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

from __future__ import annotations

from typing import TYPE_CHECKING

from core.languages.python.code_repair.code_repair import code_repair
from core.languages.python.explanations.explanations import explain_optimizations
from core.languages.python.jit_rewrite.jit_rewrite import jit_rewrite
from core.languages.python.optimization_review.optimization_review import get_optimization_review
from core.languages.python.optimizer.optimizer import optimize_python
from core.languages.python.testgen.testgen import testgen_python

if TYPE_CHECKING:
    from aiservice.llm 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.explanations import (
        ExplanationsErrorResponseSchema,
        ExplanationsResponseSchema,
        ExplanationsSchema,
    )
    from core.languages.python.optimization_review.optimization_review import (
        OptimizationReviewErrorSchema,
        OptimizationReviewResponseSchema,
        OptimizationReviewSchema,
    )
    from core.languages.python.optimizer.context_utils.optimizer_context import (
        OptimizeErrorResponseSchema,
        OptimizeResponseSchema,
    )
    from core.languages.python.optimizer.models import JitRewriteOptimizeSchema, OptimizeSchema
    from core.languages.python.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."""
        return await testgen_python(request, data)

    async def optimizer_optimize(
        self, request: AuthenticatedRequest, data: OptimizeSchema
    ) -> tuple[int, OptimizeResponseSchema | OptimizeErrorResponseSchema]:
        """Optimize Python code for performance."""
        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."""
        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."""
        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."""
        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."""
        kwargs = {}
        if explanations_model is not None:
            kwargs["explanations_model"] = explanations_model
        return await explain_optimizations(user_id, data, **kwargs)
