# [DEF:backend.src.services.clean_release.repository:Module]
# @TIER: STANDARD
# @SEMANTICS: clean-release, repository, persistence, in-memory
# @PURPOSE: Provide repository adapter for clean release entities with deterministic access methods.
# @LAYER: Infra
# @RELATION: DEPENDS_ON -> backend.src.models.clean_release
# @INVARIANT: Repository operations are side-effect free outside explicit save/update calls.

from __future__ import annotations

from dataclasses import dataclass, field
from typing import Dict, List, Optional

from ...models.clean_release import (
    CleanProfilePolicy,
    ComplianceCheckRun,
    ComplianceReport,
    ComplianceViolation,
    DistributionManifest,
    ReleaseCandidate,
    ResourceSourceRegistry,
)


# [DEF:CleanReleaseRepository:Class]
# @PURPOSE: Data access object for clean release lifecycle.
@dataclass
class CleanReleaseRepository:
    candidates: Dict[str, ReleaseCandidate] = field(default_factory=dict)
    policies: Dict[str, CleanProfilePolicy] = field(default_factory=dict)
    registries: Dict[str, ResourceSourceRegistry] = field(default_factory=dict)
    manifests: Dict[str, DistributionManifest] = field(default_factory=dict)
    check_runs: Dict[str, ComplianceCheckRun] = field(default_factory=dict)
    reports: Dict[str, ComplianceReport] = field(default_factory=dict)
    violations: Dict[str, ComplianceViolation] = field(default_factory=dict)

    def save_candidate(self, candidate: ReleaseCandidate) -> ReleaseCandidate:
        self.candidates[candidate.candidate_id] = candidate
        return candidate

    def get_candidate(self, candidate_id: str) -> Optional[ReleaseCandidate]:
        return self.candidates.get(candidate_id)

    def save_policy(self, policy: CleanProfilePolicy) -> CleanProfilePolicy:
        self.policies[policy.policy_id] = policy
        return policy

    def get_policy(self, policy_id: str) -> Optional[CleanProfilePolicy]:
        return self.policies.get(policy_id)

    def get_active_policy(self) -> Optional[CleanProfilePolicy]:
        for policy in self.policies.values():
            if policy.active:
                return policy
        return None

    def save_registry(self, registry: ResourceSourceRegistry) -> ResourceSourceRegistry:
        self.registries[registry.registry_id] = registry
        return registry

    def get_registry(self, registry_id: str) -> Optional[ResourceSourceRegistry]:
        return self.registries.get(registry_id)

    def save_manifest(self, manifest: DistributionManifest) -> DistributionManifest:
        self.manifests[manifest.manifest_id] = manifest
        return manifest

    def get_manifest(self, manifest_id: str) -> Optional[DistributionManifest]:
        return self.manifests.get(manifest_id)

    def save_check_run(self, check_run: ComplianceCheckRun) -> ComplianceCheckRun:
        self.check_runs[check_run.check_run_id] = check_run
        return check_run

    def get_check_run(self, check_run_id: str) -> Optional[ComplianceCheckRun]:
        return self.check_runs.get(check_run_id)

    def save_report(self, report: ComplianceReport) -> ComplianceReport:
        self.reports[report.report_id] = report
        return report

    def get_report(self, report_id: str) -> Optional[ComplianceReport]:
        return self.reports.get(report_id)

    def save_violation(self, violation: ComplianceViolation) -> ComplianceViolation:
        self.violations[violation.violation_id] = violation
        return violation

    def get_violations_by_check_run(self, check_run_id: str) -> List[ComplianceViolation]:
        return [v for v in self.violations.values() if v.check_run_id == check_run_id]
    def clear_history(self) -> None:
        self.check_runs.clear()
        self.reports.clear()
        self.violations.clear()
# [/DEF:CleanReleaseRepository:Class]
# [/DEF:backend.src.services.clean_release.repository:Module]