# #region Test.CleanRelease.Mappers [C:2] [TYPE Module]
# @RELATION  BINDS_TO -> [clean_release_mappers]
# @SEMANTICS: tests, clean-release, mapper, dto, transform
# @PURPOSE: Validate domain-to-DTO mapping functions for clean release entities.

from __future__ import annotations

from datetime import UTC, datetime
from unittest.mock import MagicMock

import pytest

from src.models.clean_release import ComplianceReport, ComplianceRun, DistributionManifest, ReleaseCandidate
from src.services.clean_release.enums import CandidateStatus, ComplianceDecision, RunStatus


# #region test_map_candidate_to_dto [C:2] [TYPE Function]
def test_map_candidate_to_dto():
    from src.services.clean_release.mappers import map_candidate_to_dto

    candidate = ReleaseCandidate(
        id="cand-1",
        version="1.0.0",
        source_snapshot_ref="git:sha1",
        created_by="operator",
        created_at=datetime.now(UTC),
        status=CandidateStatus.DRAFT.value,
        build_id="build-123",
    )

    dto = map_candidate_to_dto(candidate)
    assert dto.id == "cand-1"
    assert dto.version == "1.0.0"
    assert dto.source_snapshot_ref == "git:sha1"
    assert dto.build_id == "build-123"
    assert dto.status == CandidateStatus.DRAFT
# #endregion test_map_candidate_to_dto


# #region test_map_manifest_to_dto [C:2] [TYPE Function]
def test_map_manifest_to_dto():
    from src.services.clean_release.mappers import map_manifest_to_dto

    manifest = DistributionManifest(
        id="manifest-1",
        candidate_id="cand-1",
        manifest_version=1,
        manifest_digest="digest-1",
        artifacts_digest="art-digest-1",
        created_at=datetime.now(UTC),
        created_by="operator",
        source_snapshot_ref="git:sha1",
        content_json={"summary": {"total": 10}},
    )

    dto = map_manifest_to_dto(manifest)
    assert dto.id == "manifest-1"
    assert dto.candidate_id == "cand-1"
    assert dto.manifest_version == 1
    assert dto.manifest_digest == "digest-1"
    assert dto.content_json == {"summary": {"total": 10}}
# #endregion test_map_manifest_to_dto


# #region test_map_run_to_dto [C:2] [TYPE Function]
def test_map_run_to_dto():
    from src.services.clean_release.mappers import map_run_to_dto

    run = ComplianceRun(
        id="run-1",
        candidate_id="cand-1",
        manifest_id="manifest-1",
        manifest_digest="digest-1",
        policy_snapshot_id="policy-1",
        registry_snapshot_id="registry-1",
        requested_by="operator",
        requested_at=datetime.now(UTC),
        started_at=datetime.now(UTC),
        status=RunStatus.SUCCEEDED.value,
        final_status=ComplianceDecision.PASSED.value,
        task_id="task-42",
    )

    dto = map_run_to_dto(run)
    assert dto.run_id == "run-1"
    assert dto.candidate_id == "cand-1"
    assert dto.status == RunStatus.SUCCEEDED
    assert dto.final_status == ComplianceDecision.PASSED
    assert dto.task_id == "task-42"
# #endregion test_map_run_to_dto


# #region test_map_run_to_dto_no_final_status [C:2] [TYPE Function]
def test_map_run_to_dto_no_final_status():
    from src.services.clean_release.mappers import map_run_to_dto

    run = ComplianceRun(
        id="run-2",
        candidate_id="cand-1",
        manifest_id="manifest-1",
        manifest_digest="digest-1",
        policy_snapshot_id="policy-1",
        registry_snapshot_id="registry-1",
        requested_by="operator",
        requested_at=datetime.now(UTC),
        started_at=datetime.now(UTC),
        status=RunStatus.RUNNING.value,
    )

    dto = map_run_to_dto(run)
    assert dto.run_id == "run-2"
    assert dto.final_status is None
# #endregion test_map_run_to_dto_no_final_status


# #region test_map_report_to_dto [C:2] [TYPE Function]
def test_map_report_to_dto():
    from src.services.clean_release.mappers import map_report_to_dto

    report = ComplianceReport(
        id="CCR-1",
        run_id="run-1",
        candidate_id="cand-1",
        final_status=ComplianceDecision.PASSED.value,
        summary_json={"operator_summary": "OK", "violations_count": 0, "blocking_violations_count": 0},
        generated_at=datetime.now(UTC),
        immutable=True,
    )

    dto = map_report_to_dto(report)
    assert dto.report_id == "CCR-1"
    assert dto.candidate_id == "cand-1"
    assert dto.final_status == ComplianceDecision.PASSED
    assert dto.generated_at is not None
# #endregion test_map_report_to_dto


# #region test_map_report_to_dto_blocked [C:2] [TYPE Function]
def test_map_report_to_dto_blocked():
    from src.services.clean_release.mappers import map_report_to_dto

    report = ComplianceReport(
        id="CCR-2",
        run_id="run-2",
        candidate_id="cand-2",
        final_status=ComplianceDecision.BLOCKED.value,
        summary_json={"operator_summary": "BLOCKED", "violations_count": 3, "blocking_violations_count": 2},
        generated_at=datetime.now(UTC),
        immutable=True,
    )

    dto = map_report_to_dto(report)
    assert dto.final_status == ComplianceDecision.BLOCKED
# #endregion test_map_report_to_dto_blocked


# #endregion Test.CleanRelease.Mappers
