from sqlalchemy.orm import Session
from app.infrastructure.database.models.trucks import Truck


class TrucksRepository:
    def __init__(self, db: Session):
        self.db = db

    def get_all(self):
        return self.db.query(Truck).all()

    def get_by_id(self, truck_id: int):
        return self.db.query(Truck).filter(Truck.id == truck_id).first()

    def create(self, truck: Truck):
        self.db.add(truck)
        self.db.commit()
        self.db.refresh(truck)
        return truck

    def update(self, truck: Truck):
        self.db.commit()
        return truck

    def delete(self, truck_id: int):
        truck = self.db.query(Truck).filter(Truck.id == truck_id).first()
        if truck:
            self.db.delete(truck)
            self.db.commit()
            return truck
        return None
