"""
Routes for PedimentoTransportMeans CRUD operations
"""

from typing import Any, Dict, List

from core.database import get_core_db
from core.security import get_current_user, validate_access_to_resource
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.orm import Session

from ..dtos.pedimento_transport_means import (
    PedimentoTransportMeansCreate,
    PedimentoTransportMeansResponse,
    PedimentoTransportMeansUpdate,
)
from ..services.pedimento_transport_means import PedimentoTransportMeansService

router = APIRouter(prefix="/{pedimento_id}/transport-means")


@router.get("/", response_model=List[PedimentoTransportMeansResponse])
async def list_transport_means(
    pedimento_id: int,
    company_id: int = Query(..., description="Company ID"),
    db: Session = Depends(get_core_db),
    current_user: Dict[str, Any] = Depends(get_current_user),
):
    """Get all transport means for a pedimento"""
    tenant_id = validate_access_to_resource(
        db, company_id, current_user, ["pedimentos_mgmt.view"]
    )

    transport_means = PedimentoTransportMeansService.get_by_pedimento_id(
        db, pedimento_id, tenant_id, company_id
    )
    return transport_means


@router.get("/{transport_mean_id}", response_model=PedimentoTransportMeansResponse)
async def get_transport_mean(
    pedimento_id: int,
    transport_mean_id: int,
    company_id: int = Query(..., description="Company ID"),
    db: Session = Depends(get_core_db),
    current_user: Dict[str, Any] = Depends(get_current_user),
):
    """Get a specific transport mean by ID"""
    tenant_id = validate_access_to_resource(
        db, company_id, current_user, ["pedimentos_mgmt.view"]
    )

    transport_mean = PedimentoTransportMeansService.get_by_id(
        db, transport_mean_id, pedimento_id, tenant_id, company_id
    )
    if not transport_mean:
        raise HTTPException(status_code=404, detail="Transport mean not found")

    return transport_mean


@router.post("/", response_model=PedimentoTransportMeansResponse, status_code=201)
async def create_transport_mean(
    pedimento_id: int,
    data: PedimentoTransportMeansCreate,
    company_id: int = Query(..., description="Company ID"),
    db: Session = Depends(get_core_db),
    current_user: Dict[str, Any] = Depends(get_current_user),
):
    """Create a new transport mean"""
    tenant_id = validate_access_to_resource(
        db, company_id, current_user, ["pedimentos_mgmt.edit"]
    )

    # Ensure pedimento_id matches
    if data.pedimento_id != pedimento_id:
        raise HTTPException(status_code=400, detail="Pedimento ID mismatch")

    transport_mean = PedimentoTransportMeansService.create(
        db, data, tenant_id, company_id
    )
    return transport_mean


@router.put("/{transport_mean_id}", response_model=PedimentoTransportMeansResponse)
async def update_transport_mean(
    pedimento_id: int,
    transport_mean_id: int,
    data: PedimentoTransportMeansUpdate,
    company_id: int = Query(..., description="Company ID"),
    db: Session = Depends(get_core_db),
    current_user: Dict[str, Any] = Depends(get_current_user),
):
    """Update a transport mean"""
    tenant_id = validate_access_to_resource(
        db, company_id, current_user, ["pedimentos_mgmt.edit"]
    )

    transport_mean = PedimentoTransportMeansService.update(
        db, transport_mean_id, pedimento_id, tenant_id, company_id, data
    )
    if not transport_mean:
        raise HTTPException(status_code=404, detail="Transport mean not found")

    return transport_mean


@router.delete("/{transport_mean_id}", status_code=204)
async def delete_transport_mean(
    pedimento_id: int,
    transport_mean_id: int,
    company_id: int = Query(..., description="Company ID"),
    db: Session = Depends(get_core_db),
    current_user: Dict[str, Any] = Depends(get_current_user),
):
    """Delete a transport mean"""
    tenant_id = validate_access_to_resource(
        db, company_id, current_user, ["pedimentos_mgmt.edit"]
    )

    success = PedimentoTransportMeansService.delete(
        db, transport_mean_id, pedimento_id, tenant_id, company_id
    )
    if not success:
        raise HTTPException(status_code=404, detail="Transport mean not found")

    return None
