"""Sales activity 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 (
    SalesActivityCreate,
    SalesActivityRead,
    SalesActivityUpdate,
)
from app.services.foundation import SalesActivityService
from shared.pagination import PaginationParams
from shared.security import CurrentUser

router = APIRouter()


@router.post("", response_model=SalesActivityRead, status_code=status.HTTP_201_CREATED)
async def create_activity(
    body: SalesActivityCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await SalesActivityService(db).create(tenant_id, body)


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


@router.get("/{activity_id}", response_model=SalesActivityRead)
async def get_activity(
    activity_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await SalesActivityService(db).get(tenant_id, activity_id)


@router.patch("/{activity_id}", response_model=SalesActivityRead)
async def update_activity(
    activity_id: UUID,
    body: SalesActivityUpdate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await SalesActivityService(db).update(tenant_id, activity_id, body)


@router.post("/{activity_id}/complete", response_model=SalesActivityRead)
async def complete_activity(
    activity_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await SalesActivityService(db).complete(tenant_id, activity_id)
