# #region compliance_repository [C:3] [TYPE Module] [SEMANTICS clean-release, compliance, repository, persistence, crud]
# @BRIEF Persist and query compliance runs, stage runs, and violations.
# @LAYER Infrastructure
# @RELATION DEPENDS_ON -> [EXT:Library:sqlalchemy]


from sqlalchemy.orm import Session

from src.core.logger import belief_scope
from src.models.clean_release import ComplianceRun, ComplianceStageRun, ComplianceViolation


class ComplianceRepository:
    """
    @PURPOSE: Encapsulates database operations for Compliance execution records.
    """
    def __init__(self, db: Session):
        self.db = db

    def save_run(self, run: ComplianceRun) -> ComplianceRun:
        """
        @PURPOSE: Persist a compliance run.
        """
        with belief_scope("ComplianceRepository.save_run"):
            self.db.add(run)
            self.db.commit()
            self.db.refresh(run)
            return run

    def get_run(self, run_id: str) -> ComplianceRun | None:
        """
        @PURPOSE: Retrieve a compliance run by ID.
        """
        with belief_scope("ComplianceRepository.get_run"):
            return self.db.query(ComplianceRun).filter(ComplianceRun.id == run_id).first()

    def list_runs_by_candidate(self, candidate_id: str) -> list[ComplianceRun]:
        """
        @PURPOSE: List all runs for a specific candidate.
        """
        with belief_scope("ComplianceRepository.list_runs_by_candidate"):
            return self.db.query(ComplianceRun).filter(ComplianceRun.candidate_id == candidate_id).all()

    def save_stage_run(self, stage_run: ComplianceStageRun) -> ComplianceStageRun:
        """
        @PURPOSE: Persist a stage execution record.
        """
        with belief_scope("ComplianceRepository.save_stage_run"):
            self.db.add(stage_run)
            self.db.commit()
            self.db.refresh(stage_run)
            return stage_run

    def list_stages_by_run(self, run_id: str) -> list[ComplianceStageRun]:
        """
        @PURPOSE: List all stage runs for a specific compliance run.
        """
        with belief_scope("ComplianceRepository.list_stages_by_run"):
            return self.db.query(ComplianceStageRun).filter(ComplianceStageRun.run_id == run_id).all()

    def save_violation(self, violation: ComplianceViolation) -> ComplianceViolation:
        """
        @PURPOSE: Persist a compliance violation.
        """
        with belief_scope("ComplianceRepository.save_violation"):
            self.db.add(violation)
            self.db.commit()
            self.db.refresh(violation)
            return violation

    def save_violations(self, violations: list[ComplianceViolation]) -> list[ComplianceViolation]:
        """
        @PURPOSE: Persist multiple violations.
        """
        with belief_scope("ComplianceRepository.save_violations"):
            self.db.add_all(violations)
            self.db.commit()
            for v in violations:
                self.db.refresh(v)
            return violations

    def list_violations_by_run(self, run_id: str) -> list[ComplianceViolation]:
        """
        @PURPOSE: List all violations for a specific compliance run.
        """
        with belief_scope("ComplianceRepository.list_violations_by_run"):
            return self.db.query(ComplianceViolation).filter(ComplianceViolation.run_id == run_id).all()

# #endregion compliance_repository
