from sqlalchemy.orm import Session

from . import dto, models


class TrailerTypeService:
    @staticmethod
    def get_trailer_type_by_key(db: Session, trailer_type_key: str):
        return (
            db.query(models.TrailerType)
            .filter(models.TrailerType.trailer_type_key == trailer_type_key)
            .first()
        )

    @staticmethod
    def create_trailer_type(db: Session, trailer_type_data: dto.TrailerTypeCreateDTO):
        new_trailer_type = models.TrailerType(**trailer_type_data.dict())
        db.add(new_trailer_type)
        db.commit()
        db.refresh(new_trailer_type)
        return new_trailer_type

    @staticmethod
    def delete_trailer_type(db: Session, trailer_type_key: str):
        trailer_type = TrailerTypeService.get_trailer_type_by_key(db, trailer_type_key)
        if trailer_type:
            db.delete(trailer_type)
            db.commit()
        return trailer_type
