# #region Test.Repository.Coverage [C:2] [TYPE Module] [SEMANTICS test,clean-release,repository,coverage]
# @BRIEF Coverage tests for CleanReleaseRepository — alias methods, clear_history, _run_ref.
# @RELATION BINDS_TO -> [RepositoryRelations]
# @TEST_EDGE: save_distribution_manifest -> alias delegates to save_manifest
# @TEST_EDGE: get_distribution_manifest -> alias delegates to get_manifest
# @TEST_EDGE: save_compliance_run -> alias delegates to save_check_run
# @TEST_EDGE: get_compliance_run -> alias delegates to get_check_run
# @TEST_EDGE: clear_history -> clears runs, reports, violations

from __future__ import annotations

from datetime import UTC, datetime

from src.models.clean_release import (
    ComplianceRun,
    ComplianceReport,
    ComplianceStageRun,
    ComplianceViolation,
    DistributionManifest,
    ReleaseCandidate,
)
from src.services.clean_release.repository import CleanReleaseRepository


# #region test_save_distribution_manifest_alias [C:1] [TYPE Function]
def test_save_distribution_manifest_alias():
    """save_distribution_manifest delegates to save_manifest."""
    repo = CleanReleaseRepository()
    m = DistributionManifest(
        id="dm-alias-1", candidate_id="cand-1", manifest_version=1,
        manifest_digest="d1", artifacts_digest="d1",
        source_snapshot_ref="ref", content_json={},
        created_by="tester", created_at=datetime.now(UTC), immutable=True,
    )
    result = repo.save_distribution_manifest(m)
    assert result.id == "dm-alias-1"
    assert repo.manifests["dm-alias-1"] is m
# #endregion test_save_distribution_manifest_alias


# #region test_get_distribution_manifest_alias [C:1] [TYPE Function]
def test_get_distribution_manifest_alias():
    """get_distribution_manifest delegates to get_manifest."""
    repo = CleanReleaseRepository()
    m = DistributionManifest(
        id="dm-alias-2", candidate_id="cand-1", manifest_version=1,
        manifest_digest="d2", artifacts_digest="d2",
        source_snapshot_ref="ref", content_json={},
        created_by="tester", created_at=datetime.now(UTC), immutable=True,
    )
    repo.save_manifest(m)
    result = repo.get_distribution_manifest("dm-alias-2")
    assert result is m

    missing = repo.get_distribution_manifest("nonexistent")
    assert missing is None
# #endregion test_get_distribution_manifest_alias


# #region test_save_compliance_run_alias [C:1] [TYPE Function]
def test_save_compliance_run_alias():
    """save_compliance_run delegates to save_check_run."""
    repo = CleanReleaseRepository()
    r = ComplianceRun(
        id="cr-alias-1", candidate_id="cand-1", manifest_id="m1",
        manifest_digest="d1", policy_snapshot_id="p1", registry_snapshot_id="reg1",
        requested_by="tester", requested_at=datetime.now(UTC),
        status="RUNNING",
    )
    result = repo.save_compliance_run(r)
    assert result.id == "cr-alias-1"
    assert repo.check_runs["cr-alias-1"] is r
# #endregion test_save_compliance_run_alias


# #region test_get_compliance_run_alias [C:1] [TYPE Function]
def test_get_compliance_run_alias():
    """get_compliance_run delegates to get_check_run."""
    repo = CleanReleaseRepository()
    r = ComplianceRun(
        id="cr-alias-2", candidate_id="cand-1", manifest_id="m1",
        manifest_digest="d1", policy_snapshot_id="p1", registry_snapshot_id="reg1",
        requested_by="tester", requested_at=datetime.now(UTC),
        status="RUNNING",
    )
    repo.save_check_run(r)
    result = repo.get_compliance_run("cr-alias-2")
    assert result is r

    missing = repo.get_compliance_run("nonexistent")
    assert missing is None
# #endregion test_get_compliance_run_alias


# #region test_clear_history_clears_evidence [C:1] [TYPE Function]
def test_clear_history_clears_evidence():
    """clear_history removes check_runs, reports, and violations but keeps candidates."""
    repo = CleanReleaseRepository()

    repo.save_candidate(ReleaseCandidate(
        id="cand-keep-1", version="1.0.0", source_snapshot_ref="ref",
        created_by="tester", created_at=datetime.now(UTC), status="DRAFT",
    ))
    repo.save_check_run(ComplianceRun(
        id="run-clear-1", candidate_id="cand-keep-1", manifest_id="m1",
        manifest_digest="d1", policy_snapshot_id="p1", registry_snapshot_id="reg1",
        requested_by="tester", requested_at=datetime.now(UTC), status="RUNNING",
    ))
    repo.save_report(ComplianceReport(
        id="rpt-clear-1", run_id="run-clear-1", candidate_id="cand-keep-1",
        final_status="PASSED", summary_json={}, generated_at=datetime.now(UTC), immutable=True,
    ))
    repo.save_violation(ComplianceViolation(
        id="viol-clear-1", run_id="run-clear-1", stage_name="DATA_PURITY",
        code="EXT", severity="MAJOR", message="violation",
    ))

    repo.clear_history()

    assert len(repo.check_runs) == 0
    assert len(repo.reports) == 0
    assert len(repo.violations) == 0
    assert len(repo.candidates) == 1  # Candidates are preserved
# #endregion test_clear_history_clears_evidence


# #region test_run_ref_extra_helper [C:1] [TYPE Function]
def test_run_ref_extra_helper():
    """_run_ref extracts run_id from entity."""
    from src.services.clean_release.repository import CleanReleaseRepository
    from src.models.clean_release import ComplianceViolation

    repo = CleanReleaseRepository()
    v = ComplianceViolation(
        id="viol-runref", run_id="run-123", stage_name="DATA_PURITY",
        code="EXT", severity="MAJOR", message="test",
    )
    ref = repo._run_ref(v)
    assert ref == "run-123"
# #endregion test_run_ref_extra_helper
# #endregion Test.Repository.Coverage
