"""Logs domain — HTTP routes."""
from __future__ import annotations

from uuid import UUID

from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession

from src.core.database import get_db
from src.core.dependencies import require_active_billing_for_project_id
from src.domain.auth.models import User
from src.domain.auth.router import get_current_user
from src.domain.logs.repository import ProjectLogConfigRepository, ProjectLogDrainRepository
from src.domain.logs.schemas import (
    CreateLogDrainRequest,
    LogConfigResponse,
    LogDrainResponse,
    TestDrainResponse,
    UpdateLogConfigRequest,
    UpdateLogDrainRequest,
)
from src.domain.logs.service import LogsService
from src.integrations.cloud_control_client import CloudControlClient

router = APIRouter(
    prefix="/api/projects/{project_id}/logs",
    tags=["logs"],
)


def _get_service(db: AsyncSession = Depends(get_db)) -> LogsService:
    return LogsService(
        config_repo=ProjectLogConfigRepository(db),
        drain_repo=ProjectLogDrainRepository(db),
        cloud_client=CloudControlClient(),
    )


@router.get("/config", response_model=LogConfigResponse)
async def get_log_config(
    project_id: UUID = Depends(require_active_billing_for_project_id),
    _: User = Depends(get_current_user),
    svc: LogsService = Depends(_get_service),
) -> LogConfigResponse:
    return await svc.get_or_create_config(project_id)


@router.put("/config", response_model=LogConfigResponse)
async def update_log_config(
    body: UpdateLogConfigRequest,
    project_id: UUID = Depends(require_active_billing_for_project_id),
    _: User = Depends(get_current_user),
    svc: LogsService = Depends(_get_service),
) -> LogConfigResponse:
    return await svc.update_config(project_id, body)


@router.get("/drains", response_model=list[LogDrainResponse])
async def list_drains(
    project_id: UUID = Depends(require_active_billing_for_project_id),
    _: User = Depends(get_current_user),
    svc: LogsService = Depends(_get_service),
) -> list[LogDrainResponse]:
    return await svc.list_drains(project_id)


@router.post("/drains", response_model=LogDrainResponse, status_code=201)
async def create_drain(
    body: CreateLogDrainRequest,
    project_id: UUID = Depends(require_active_billing_for_project_id),
    _: User = Depends(get_current_user),
    svc: LogsService = Depends(_get_service),
) -> LogDrainResponse:
    return await svc.create_drain(project_id, body)


@router.patch("/drains/{drain_id}", response_model=LogDrainResponse)
async def update_drain(
    drain_id: UUID,
    body: UpdateLogDrainRequest,
    project_id: UUID = Depends(require_active_billing_for_project_id),
    _: User = Depends(get_current_user),
    svc: LogsService = Depends(_get_service),
) -> LogDrainResponse:
    return await svc.update_drain(project_id, drain_id, body)


@router.delete("/drains/{drain_id}", status_code=204, response_model=None)
async def delete_drain(
    drain_id: UUID,
    project_id: UUID = Depends(require_active_billing_for_project_id),
    _: User = Depends(get_current_user),
    svc: LogsService = Depends(_get_service),
) -> None:
    await svc.delete_drain(project_id, drain_id)


@router.post("/drains/{drain_id}/test", response_model=TestDrainResponse)
async def test_drain(
    drain_id: UUID,
    project_id: UUID = Depends(require_active_billing_for_project_id),
    _: User = Depends(get_current_user),
    svc: LogsService = Depends(_get_service),
) -> TestDrainResponse:
    return await svc.test_drain(project_id, drain_id)
