"""Pipeline and stage CRM APIs."""
from __future__ import annotations

from uuid import UUID

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

from app.api.deps import get_db, get_pagination, require_tenant
from app.core.security import get_current_user
from app.schemas.foundation import (
    PipelineCreate,
    PipelineRead,
    PipelineStageCreate,
    PipelineStageRead,
    PipelineStageUpdate,
    PipelineUpdate,
)
from app.services.foundation import PipelineService
from shared.pagination import PaginationParams
from shared.security import CurrentUser

router = APIRouter()


@router.post("", response_model=PipelineRead, status_code=status.HTTP_201_CREATED)
async def create_pipeline(
    body: PipelineCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await PipelineService(db).create(tenant_id, body)


@router.get("", response_model=list[PipelineRead])
async def list_pipelines(
    tenant_id: UUID = Depends(require_tenant),
    pagination: PaginationParams = Depends(get_pagination),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await PipelineService(db).list(
        tenant_id, offset=pagination.offset, limit=pagination.page_size
    )


@router.post(
    "/stages", response_model=PipelineStageRead, status_code=status.HTTP_201_CREATED
)
async def create_stage(
    body: PipelineStageCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await PipelineService(db).create_stage(tenant_id, body)


@router.get("/stages/{stage_id}", response_model=PipelineStageRead)
async def get_stage(
    stage_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await PipelineService(db).get_stage(tenant_id, stage_id)


@router.patch("/stages/{stage_id}", response_model=PipelineStageRead)
async def update_stage(
    stage_id: UUID,
    body: PipelineStageUpdate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await PipelineService(db).update_stage(tenant_id, stage_id, body)


@router.get("/{pipeline_id}", response_model=PipelineRead)
async def get_pipeline(
    pipeline_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await PipelineService(db).get(tenant_id, pipeline_id)


@router.patch("/{pipeline_id}", response_model=PipelineRead)
async def update_pipeline(
    pipeline_id: UUID,
    body: PipelineUpdate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await PipelineService(db).update(tenant_id, pipeline_id, body)


@router.get("/{pipeline_id}/stages", response_model=list[PipelineStageRead])
async def list_stages(
    pipeline_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    pagination: PaginationParams = Depends(get_pagination),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await PipelineService(db).list_stages(
        tenant_id, pipeline_id, offset=pagination.offset, limit=pagination.page_size
    )
