from __future__ import annotations

from typing import Any, Awaitable, Callable

from fastapi import APIRouter, Depends

from app.models import ApiResponse, MvpIncidentIngestRequest, MvpIocEvaluateRequest, MvpVpnEvaluateRequest
from app.services.mvp_service import MvpService


def build_mvp_router(
    mvp_service: MvpService,
    require_internal_key: Callable[..., Awaitable[None]],
) -> APIRouter:
    router = APIRouter(prefix="/mvp", tags=["mvp"])

    @router.post("/incidents/ingest", response_model=ApiResponse, dependencies=[Depends(require_internal_key)])
    async def ingest_incident(payload: MvpIncidentIngestRequest) -> ApiResponse:
        result = await mvp_service.ingest_incident(payload.model_dump(mode="json"))
        return ApiResponse(data=result)

    @router.post("/ioc/evaluate", response_model=ApiResponse, dependencies=[Depends(require_internal_key)])
    async def evaluate_ioc(payload: MvpIocEvaluateRequest) -> ApiResponse:
        result = await mvp_service.evaluate_ioc(payload.model_dump(mode="json"))
        return ApiResponse(data=result)

    @router.post("/vpn/evaluate", response_model=ApiResponse, dependencies=[Depends(require_internal_key)])
    async def evaluate_vpn(payload: MvpVpnEvaluateRequest) -> ApiResponse:
        result = await mvp_service.evaluate_vpn(payload.model_dump(mode="json"))
        return ApiResponse(data=result)

    @router.get("/config/policies", response_model=ApiResponse)
    async def get_policies() -> ApiResponse:
        data = mvp_service.repo.get_policy()
        return ApiResponse(data={"policy": data})

    @router.put("/config/policies", response_model=ApiResponse, dependencies=[Depends(require_internal_key)])
    async def update_policies(payload: dict[str, Any]) -> ApiResponse:
        data = mvp_service.repo.update_policy(payload)
        return ApiResponse(data={"policy": data})

    @router.get("/health/dependencies", response_model=ApiResponse)
    async def dependency_health() -> ApiResponse:
        data = await mvp_service.dependency_health()
        return ApiResponse(data={"dependencies": data})

    return router
