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 (
    QuoteCreate,
    QuoteItemCreate,
    QuoteItemResponse,
    QuoteItemUpdate,
    QuoteResponse,
    QuoteUpdate,
)

router = APIRouter()


def _user_id(current_user: dict) -> str | None:
    return current_user.get("sub") or current_user.get("id")


@router.get("/quotes", response_model=list[QuoteResponse])
def list_quotes(
    company_id: int = Query(..., description="Company ID"),
    search: str | None = Query(None),
    quote_status: str | None = Query(None, alias="status"),
    account_id: int | None = Query(None),
    current_user: dict = Depends(get_current_user),
    db: Session = Depends(get_core_db),
):
    tenant_id = current_user["tenant_id"]
    return service.get_quotes(db, tenant_id, company_id, search, quote_status, account_id)


@router.get("/quotes/{quote_id}", response_model=QuoteResponse)
def get_quote(
    quote_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_quote(db, quote_id, tenant_id, company_id)


@router.post("/quotes", response_model=QuoteResponse, status_code=status.HTTP_201_CREATED)
def create_quote(
    payload: QuoteCreate,
    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_quote(db, payload, tenant_id, company_id, _user_id(current_user))


@router.patch("/quotes/{quote_id}", response_model=QuoteResponse)
def update_quote(
    quote_id: int,
    payload: QuoteUpdate,
    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_quote(db, quote_id, payload, tenant_id, company_id, _user_id(current_user))


@router.patch("/quotes/{quote_id}/send", response_model=QuoteResponse)
def send_quote(
    quote_id: int,
    company_id: int = Query(..., description="Company ID"),
    current_user: dict = Depends(get_current_user),
    db: Session = Depends(get_core_db),
):
    return service.send_quote(db, quote_id, current_user["tenant_id"], company_id)


@router.patch("/quotes/{quote_id}/accept", response_model=QuoteResponse)
def accept_quote(
    quote_id: int,
    company_id: int = Query(..., description="Company ID"),
    current_user: dict = Depends(get_current_user),
    db: Session = Depends(get_core_db),
):
    return service.accept_quote(db, quote_id, current_user["tenant_id"], company_id)


@router.patch("/quotes/{quote_id}/reject", response_model=QuoteResponse)
def reject_quote(
    quote_id: int,
    company_id: int = Query(..., description="Company ID"),
    current_user: dict = Depends(get_current_user),
    db: Session = Depends(get_core_db),
):
    return service.reject_quote(db, quote_id, current_user["tenant_id"], company_id)


@router.post("/quotes/{quote_id}/clone", response_model=QuoteResponse, status_code=status.HTTP_201_CREATED)
def clone_quote(
    quote_id: int,
    company_id: int = Query(..., description="Company ID"),
    current_user: dict = Depends(get_current_user),
    db: Session = Depends(get_core_db),
):
    """Clona la cotización como borrador para re-cotizar (R-C-12)."""
    return service.clone_quote(db, quote_id, current_user["tenant_id"], company_id, _user_id(current_user))


@router.delete("/quotes/{quote_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_quote(
    quote_id: int,
    company_id: int = Query(..., description="Company ID"),
    current_user: dict = Depends(get_current_user),
    db: Session = Depends(get_core_db),
):
    service.delete_quote(db, quote_id, current_user["tenant_id"], company_id)


# ----- Conceptos de la cotización -----

@router.get("/quotes/{quote_id}/items", response_model=list[QuoteItemResponse])
def list_quote_items(
    quote_id: int,
    company_id: int = Query(..., description="Company ID"),
    current_user: dict = Depends(get_current_user),
    db: Session = Depends(get_core_db),
):
    return service.get_quote_items(db, quote_id, current_user["tenant_id"], company_id)


@router.post("/quote-items", response_model=QuoteItemResponse, status_code=status.HTTP_201_CREATED)
def create_quote_item(
    payload: QuoteItemCreate,
    company_id: int = Query(..., description="Company ID"),
    current_user: dict = Depends(get_current_user),
    db: Session = Depends(get_core_db),
):
    return service.create_quote_item(db, payload, current_user["tenant_id"], company_id)


@router.patch("/quote-items/{item_id}", response_model=QuoteItemResponse)
def update_quote_item(
    item_id: int,
    payload: QuoteItemUpdate,
    company_id: int = Query(..., description="Company ID"),
    current_user: dict = Depends(get_current_user),
    db: Session = Depends(get_core_db),
):
    return service.update_quote_item(db, item_id, payload, current_user["tenant_id"], company_id)


@router.delete("/quote-items/{item_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_quote_item(
    item_id: int,
    company_id: int = Query(..., description="Company ID"),
    current_user: dict = Depends(get_current_user),
    db: Session = Depends(get_core_db),
):
    service.delete_quote_item(db, item_id, current_user["tenant_id"], company_id)
