from sqlalchemy.orm import Session

from . import dto, models

"""
Service layer for FractionRuleOctave.
"""


class FractionRuleOctaveService:
    @staticmethod
    def get_fraction_by_permission_line(
        db: Session, permission: str, line: int, fraction: str
    ):
        return (
            db.query(models.FractionRuleOctave)
            .filter(
                models.FractionRuleOctave.permission == permission,
                models.FractionRuleOctave.line == line,
                models.FractionRuleOctave.fraction == fraction,
            )
            .first()
        )

    @staticmethod
    def create_frac(db: Session, frac_data: dto.FractionRuleOctaveCreateDTO):
        new_frac = models.FractionRuleOctave(**frac_data.model_dump())
        db.add(new_frac)
        db.commit()
        db.refresh(new_frac)
        return new_frac

    @staticmethod
    def delete_fraction(db: Session, permission: str, line: int, fraction: str):
        frac = FractionRuleOctaveService.get_fraction_by_permission_line(
            db, permission, line, fraction
        )
        if frac:
            db.delete(frac)
            db.commit()
        return frac
