# [DEF:audit_repository:Module]
# @TIER: STANDARD
# @PURPOSE: Persist and query audit logs for clean release operations.
# @LAYER: Infra

from typing import Optional, List
from sqlalchemy.orm import Session
from sqlalchemy import Column, String, DateTime, JSON
from src.models.mapping import Base
from src.core.logger import belief_scope
from datetime import datetime
import uuid

from src.models.clean_release import CleanReleaseAuditLog

class AuditRepository:
    """
    @PURPOSE: Encapsulates database operations for CleanReleaseAuditLog.
    """
    def __init__(self, db: Session):
        self.db = db

    def log(self, action: str, actor: str, candidate_id: Optional[str] = None, details: Optional[dict] = None) -> CleanReleaseAuditLog:
        """
        @PURPOSE: Create an audit log entry.
        """
        with belief_scope("AuditRepository.log"):
            entry = CleanReleaseAuditLog(
                action=action,
                actor=actor,
                candidate_id=candidate_id,
                details_json=details or {}
            )
            self.db.add(entry)
            self.db.commit()
            self.db.refresh(entry)
            return entry

    def list_by_candidate(self, candidate_id: str) -> List[CleanReleaseAuditLog]:
        """
        @PURPOSE: List all audit entries for a specific candidate.
        """
        with belief_scope("AuditRepository.list_by_candidate"):
            return self.db.query(CleanReleaseAuditLog).filter(CleanReleaseAuditLog.candidate_id == candidate_id).all()

# [/DEF:audit_repository:Module]