from fastapi import APIRouter, Depends, Query, status
from sqlalchemy.orm import Session
from typing import Optional, List
from utils.utils import get_db
from . import handler
from .schema import CancellationCreate, CancellationUpdate, CancellationResponse, CancellationDetailsResponse

router = APIRouter(prefix="/cancellations", tags=["Cancellations"])


@router.post(
    "/",
    response_model=CancellationResponse,
    status_code=status.HTTP_201_CREATED,
    summary="Create Cancellation",
    description="Creates a new cancellation record for a booking. Validates that the booking exists, is not already cancelled, and that the refund amount does not exceed the booking total. Returns 404 if booking or user not found, 400 if validation fails.",
)
def create_cancellation_route(data: CancellationCreate, db: Session = Depends(get_db)):
    return handler.create_cancellation(db, data)


@router.get(
    "/",
    response_model=List[CancellationResponse],
    summary="List Cancellations",
    description="Returns a paginated list of cancellations. Supports optional filters by user_id, booking_id, and refund_status. Results are ordered by creation date descending.",
)
def list_cancellations_route(
    limit: int = Query(20, ge=1, le=100),
    offset: int = Query(0, ge=0),
    user_id: Optional[str] = None,
    booking_id: Optional[str] = None,
    refund_status: Optional[str] = None,
    db: Session = Depends(get_db),
):
    return handler.list_cancellations(db, limit, offset, user_id, booking_id, refund_status)


@router.get(
    "/{id}",
    response_model=CancellationResponse,
    summary="Get Cancellation By ID",
    description="Retrieves a single cancellation record by its unique identifier. Returns 404 if the cancellation does not exist.",
)
def get_cancellation_route(id: str, db: Session = Depends(get_db)):
    return handler.get_cancellation(db, id)


@router.get(
    "/{id}/details",
    response_model=CancellationDetailsResponse,
    summary="Get Cancellation Details",
    description="Retrieves comprehensive cancellation details including booking reference, schedule information, bus details, route information, and user information. Uses eager loading to prevent N+1 queries. Returns 404 if cancellation not found.",
)
def get_cancellation_details_route(id: str, db: Session = Depends(get_db)):
    return handler.get_cancellation_details(db, id)


@router.put(
    "/{id}",
    response_model=CancellationResponse,
    summary="Update Cancellation",
    description="Updates an existing cancellation record. Only provided fields are updated; unset fields remain unchanged. Validates foreign key references if booking_id or user_id are updated. Returns 404 if cancellation not found, 400 if no fields provided.",
)
def update_cancellation_route(id: str, data: CancellationUpdate, db: Session = Depends(get_db)):
    return handler.update_cancellation(db, id, data)


@router.delete(
    "/{id}",
    status_code=status.HTTP_200_OK,
    summary="Delete Cancellation",
    description="Deletes a cancellation record by its unique identifier. Returns 404 if the cancellation does not exist.",
)
def delete_cancellation_route(id: str, db: Session = Depends(get_db)):
    return handler.delete_cancellation(db, id)