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 (
    OpportunityCreate,
    OpportunityMove,
    OpportunityResponse,
    OpportunityUpdate,
)

router = APIRouter()


@router.get("/opportunities", response_model=list[OpportunityResponse])
def list_opportunities(
    company_id: int = Query(..., description="Company ID"),
    pipeline_id: int | None = Query(None, description="Filtrar por embudo"),
    stage_id: int | None = Query(None, description="Filtrar por etapa"),
    opp_status: str | None = Query(None, alias="status", description="Filtrar por estado"),
    search: str | None = Query(None, description="Búsqueda por nombre"),
    current_user: dict = Depends(get_current_user),
    db: Session = Depends(get_core_db),
):
    tenant_id = current_user["tenant_id"]
    return service.get_opportunities(
        db, tenant_id, company_id, pipeline_id, stage_id, opp_status, search
    )


@router.get("/opportunities/{opportunity_id}", response_model=OpportunityResponse)
def get_opportunity(
    opportunity_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"]
    return service.get_opportunity(db, opportunity_id, tenant_id, company_id)


@router.post("/opportunities", response_model=OpportunityResponse, status_code=status.HTTP_201_CREATED)
def create_opportunity(
    payload: OpportunityCreate,
    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_opportunity(db, payload, tenant_id, company_id)


@router.patch("/opportunities/{opportunity_id}", response_model=OpportunityResponse)
def update_opportunity(
    opportunity_id: int,
    payload: OpportunityUpdate,
    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_opportunity(db, opportunity_id, payload, tenant_id, company_id)


@router.patch("/opportunities/{opportunity_id}/move", response_model=OpportunityResponse)
def move_opportunity(
    opportunity_id: int,
    payload: OpportunityMove,
    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.move_opportunity(db, opportunity_id, payload.stage_id, tenant_id, company_id)


@router.delete("/opportunities/{opportunity_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_opportunity(
    opportunity_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_opportunity(db, opportunity_id, tenant_id, company_id)
