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.core.exceptions import NotFoundError
from src.domain.auth.models import User
from src.domain.auth.router import get_current_user
from src.domain.livechat.repository import LivechatRepository
from src.domain.livechat.schemas import LivechatConfigResponse, UpdateLivechatConfigRequest, LivechatTranscriptPurgeResponse
from src.domain.livechat.service import LivechatService
from src.domain.projects.repository import ProjectRepository, ProjectSettingsRepository
from src.domain.cloud.service import _enabled_project_capabilities
from src.integrations.cloud_control_client import CloudControlClient

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

def _service(db: AsyncSession = Depends(get_db)) -> LivechatService:
    return LivechatService(repo=LivechatRepository(db), cloud_client=CloudControlClient())

async def _workspace_id(db: AsyncSession, project_id: UUID) -> UUID:
    project = await ProjectRepository(db).get_by_id(project_id)
    if not project:
        raise NotFoundError("Project not found")
    return project.workspace_id

async def _with_entitlement(db: AsyncSession, project_id: UUID, cfg: LivechatConfigResponse) -> LivechatConfigResponse:
    settings = await ProjectSettingsRepository(db).get_or_create(project_id)
    entitled = "livechat.enabled" in _enabled_project_capabilities(settings.settings or {})
    return cfg.model_copy(update={"entitled": entitled})

@router.get("/config", response_model=LivechatConfigResponse)
async def get_config(project_id: UUID = Depends(require_active_billing_for_project_id), _: User = Depends(get_current_user), db: AsyncSession = Depends(get_db), service: LivechatService = Depends(_service)) -> LivechatConfigResponse:
    cfg = await service.get_or_create_config(project_id, await _workspace_id(db, project_id))
    return await _with_entitlement(db, project_id, cfg)

@router.patch("/config", response_model=LivechatConfigResponse)
async def update_config(body: UpdateLivechatConfigRequest, project_id: UUID = Depends(require_active_billing_for_project_id), _: User = Depends(get_current_user), db: AsyncSession = Depends(get_db), service: LivechatService = Depends(_service)) -> LivechatConfigResponse:
    cfg = await service.update_config(project_id, await _workspace_id(db, project_id), body)
    return await _with_entitlement(db, project_id, cfg)

@router.delete("/transcripts", response_model=LivechatTranscriptPurgeResponse)
async def delete_transcripts(project_id: UUID = Depends(require_active_billing_for_project_id), _: User = Depends(get_current_user), db: AsyncSession = Depends(get_db), service: LivechatService = Depends(_service)) -> LivechatTranscriptPurgeResponse:
    await _workspace_id(db, project_id)
    return await service.purge_transcripts(project_id)
