from fastapi import APIRouter, Depends, Query, status 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 ( PipelineCreate, PipelineResponse, PipelineUpdate, StageCreate, StageResponse, StageUpdate, ) router = APIRouter() # ----- Pipelines ----- @router.get("/pipelines", response_model=list[PipelineResponse]) def list_pipelines( company_id: int = Query(..., description="Company ID"), current_user: dict = Depends(get_current_user), db: Session = Depends(get_core_db), ): tenant_id = current_user["tenant_id"] return service.get_pipelines(db, tenant_id, company_id) @router.post("/pipelines", response_model=PipelineResponse, status_code=status.HTTP_201_CREATED) def create_pipeline( payload: PipelineCreate, company_id: int = Query(..., description="Company ID"), current_user: dict = Depends(get_current_user), db: Session = Depends(get_core_db), ): tenant_id = current_user["tenant_id"] return service.create_pipeline(db, payload, tenant_id, company_id) @router.patch("/pipelines/{pipeline_id}", response_model=PipelineResponse) def update_pipeline( pipeline_id: int, payload: PipelineUpdate, company_id: int = Query(..., description="Company ID"), current_user: dict = Depends(get_current_user), db: Session = Depends(get_core_db), ): tenant_id = current_user["tenant_id"] return service.update_pipeline(db, pipeline_id, payload, tenant_id, company_id) @router.delete("/pipelines/{pipeline_id}", status_code=status.HTTP_204_NO_CONTENT) def delete_pipeline( pipeline_id: int, company_id: int = Query(..., description="Company ID"), current_user: dict = Depends(get_current_user), db: Session = Depends(get_core_db), ): tenant_id = current_user["tenant_id"] service.delete_pipeline(db, pipeline_id, tenant_id, company_id) # ----- Stages ----- @router.get("/stages", response_model=list[StageResponse]) def list_stages( company_id: int = Query(..., description="Company ID"), pipeline_id: int | None = Query(None, description="Filtrar por embudo"), current_user: dict = Depends(get_current_user), db: Session = Depends(get_core_db), ): tenant_id = current_user["tenant_id"] return service.get_stages(db, tenant_id, company_id, pipeline_id) @router.post("/stages", response_model=StageResponse, status_code=status.HTTP_201_CREATED) def create_stage( payload: StageCreate, company_id: int = Query(..., description="Company ID"), current_user: dict = Depends(get_current_user), db: Session = Depends(get_core_db), ): tenant_id = current_user["tenant_id"] return service.create_stage(db, payload, tenant_id, company_id) @router.patch("/stages/{stage_id}", response_model=StageResponse) def update_stage( stage_id: int, payload: StageUpdate, company_id: int = Query(..., description="Company ID"), current_user: dict = Depends(get_current_user), db: Session = Depends(get_core_db), ): tenant_id = current_user["tenant_id"] return service.update_stage(db, stage_id, payload, tenant_id, company_id) @router.delete("/stages/{stage_id}", status_code=status.HTTP_204_NO_CONTENT) def delete_stage( stage_id: int, company_id: int = Query(..., description="Company ID"), current_user: dict = Depends(get_current_user), db: Session = Depends(get_core_db), ): tenant_id = current_user["tenant_id"] service.delete_stage(db, stage_id, tenant_id, company_id)