# #region policy_repository [C:3] [TYPE Module] [SEMANTICS clean-release, policy, repository, persistence, crud]
# @BRIEF Persist and query policy and registry snapshots.
# @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 CleanPolicySnapshot, SourceRegistrySnapshot


class PolicyRepository:
    """
    @PURPOSE: Encapsulates database operations for Policy and Registry snapshots.
    """
    def __init__(self, db: Session):
        self.db = db

    def save_policy_snapshot(self, snapshot: CleanPolicySnapshot) -> CleanPolicySnapshot:
        """
        @PURPOSE: Persist a policy snapshot.
        """
        with belief_scope("PolicyRepository.save_policy_snapshot"):
            self.db.add(snapshot)
            self.db.commit()
            self.db.refresh(snapshot)
            return snapshot

    def get_policy_snapshot(self, snapshot_id: str) -> CleanPolicySnapshot | None:
        """
        @PURPOSE: Retrieve a policy snapshot by ID.
        """
        with belief_scope("PolicyRepository.get_policy_snapshot"):
            return self.db.query(CleanPolicySnapshot).filter(CleanPolicySnapshot.id == snapshot_id).first()

    def save_registry_snapshot(self, snapshot: SourceRegistrySnapshot) -> SourceRegistrySnapshot:
        """
        @PURPOSE: Persist a registry snapshot.
        """
        with belief_scope("PolicyRepository.save_registry_snapshot"):
            self.db.add(snapshot)
            self.db.commit()
            self.db.refresh(snapshot)
            return snapshot

    def get_registry_snapshot(self, snapshot_id: str) -> SourceRegistrySnapshot | None:
        """
        @PURPOSE: Retrieve a registry snapshot by ID.
        """
        with belief_scope("PolicyRepository.get_registry_snapshot"):
            return self.db.query(SourceRegistrySnapshot).filter(SourceRegistrySnapshot.id == snapshot_id).first()

# #endregion policy_repository
