from core.database import get_core_db
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session

from . import dto, services

router = APIRouter()


@router.get(
    "/trailer-types/{trailer_type_key}", response_model=dto.TrailerTypeResponseDTO
)
def get_trailer_type(trailer_type_key: str, db: Session = Depends(get_core_db)):
    trailer_type = services.TrailerTypeService.get_trailer_type_by_key(
        db, trailer_type_key
    )
    if not trailer_type:
        raise HTTPException(status_code=404, detail="Trailer type not found")
    return trailer_type


@router.post("/trailer-types", response_model=dto.TrailerTypeResponseDTO)
def create_trailer_type(
    trailer_type_data: dto.TrailerTypeCreateDTO, db: Session = Depends(get_core_db)
):
    return services.TrailerTypeService.create_trailer_type(db, trailer_type_data)


@router.delete(
    "/trailer-types/{trailer_type_key}", response_model=dto.TrailerTypeResponseDTO
)
def delete_trailer_type(trailer_type_key: str, db: Session = Depends(get_core_db)):
    trailer_type = services.TrailerTypeService.delete_trailer_type(db, trailer_type_key)
    if not trailer_type:
        raise HTTPException(status_code=404, detail="Trailer type not found")
    return trailer_type
