from sqlalchemy.orm import Session
from app.infrastructure.database.models.roles import Role


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

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

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

    def create(self, role: Role):
        self.db.add(role)
        self.db.commit()
        self.db.refresh(role)
        return role

    def update(self, role: Role):
        self.db.commit()
        return role

    def delete(self, role_id: int):
        role = self.db.query(Role).filter(Role.id == role_id).first()
        if role:
            self.db.delete(role)
            self.db.commit()
            return role
        return None
