from fastapi import APIRouter, Depends, Query
from sqlalchemy.orm import Session

from core.database import get_core_db
from core.security import get_current_user

from . import service
from .dto import CaseResponse, CaseWithTimeline

router = APIRouter()


def _with_timeline(db, case) -> CaseWithTimeline:
    data = CaseWithTimeline.model_validate(case)
    data.timeline = service.build_timeline(db, case)  # type: ignore[assignment]
    return data


@router.get("/cases", response_model=list[CaseResponse])
def list_cases(
    company_id: int = Query(..., description="Company ID"),
    search: str | None = Query(None),
    account_id: int | None = Query(None),
    stage: str | None = Query(None),
    current_user: dict = Depends(get_current_user),
    db: Session = Depends(get_core_db),
):
    return service.get_cases(db, current_user["tenant_id"], company_id, search, account_id, stage)


@router.get("/cases/by-ref/{reference}", response_model=CaseWithTimeline)
def get_case_by_ref(
    reference: str,
    company_id: int = Query(..., description="Company ID"),
    current_user: dict = Depends(get_current_user),
    db: Session = Depends(get_core_db),
):
    """Expediente + historia completa por su referencia (para UI y otros sistemas)."""
    case = service.get_case_by_reference(db, reference, current_user["tenant_id"], company_id)
    return _with_timeline(db, case)


@router.get("/cases/{case_id}", response_model=CaseWithTimeline)
def get_case(
    case_id: int,
    company_id: int = Query(..., description="Company ID"),
    current_user: dict = Depends(get_current_user),
    db: Session = Depends(get_core_db),
):
    case = service.get_case(db, case_id, current_user["tenant_id"], company_id)
    return _with_timeline(db, case)
