from sqlalchemy.orm import Session, joinedload
from app.infrastructure.database.models.statuses import Status


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

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

    def get_all_with_orders(self):
        return self.db.query(Status) \
            .options(joinedload(Status.total_orders)) \
            .all()

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

    def create(self, status: Status):
        self.db.add(status)
        self.db.commit()
        self.db.refresh(status)
        return status

    def update(self, status: Status):
        self.db.commit()
        return status

    def delete(self, status_id: int):
        status = self.db.query(Status).filter(Status.id == status_id).first()
        if status:
            self.db.delete(status)
            self.db.commit()
            return status
        return None
