# #region CleanReleaseV2ApiTests [TYPE Module] [C:3] [SEMANTICS test, clean-release, v2, api, contract]
# @BRIEF API contract tests for redesigned clean release endpoints.
# @LAYER Domain
# @RELATION DEPENDS_ON -> [CleanReleaseV2Api]
from fastapi.testclient import TestClient

from src.app import app
from src.services.clean_release.enums import CandidateStatus

client = TestClient(app)
# [REASON] Implementing API contract tests for candidate/artifact/manifest endpoints (T012).
# #region test_candidate_registration_contract [TYPE Function]
# @RELATION BINDS_TO -> CleanReleaseV2ApiTests
# @BRIEF Validate candidate registration endpoint creates a draft candidate with expected identifier contract.
def test_candidate_registration_contract():
    """
    @TEST_SCENARIO: candidate_registration -> Should return 201 and candidate DTO.
    @TEST_CONTRACT: POST /api/v2/clean-release/candidates -> CandidateDTO
    """
    payload = {
        "id": "rc-test-001",
        "version": "1.0.0",
        "source_snapshot_ref": "git:sha123",
        "created_by": "test-user",
    }
    response = client.post("/api/v2/clean-release/candidates", json=payload)
    assert response.status_code == 201
    data = response.json()
    assert data["id"] == "rc-test-001"
    assert data["status"] == CandidateStatus.DRAFT.value
# #endregion test_candidate_registration_contract
# #region test_artifact_import_contract [TYPE Function]
# @RELATION BINDS_TO -> CleanReleaseV2ApiTests
# @BRIEF Validate artifact import endpoint accepts candidate artifacts and returns success status payload.
def test_artifact_import_contract():
    """
    @TEST_SCENARIO: artifact_import -> Should return 200 and success status.
    @TEST_CONTRACT: POST /api/v2/clean-release/candidates/{id}/artifacts -> SuccessDTO
    """
    candidate_id = "rc-test-001-art"
    bootstrap_candidate = {
        "id": candidate_id,
        "version": "1.0.0",
        "source_snapshot_ref": "git:sha123",
        "created_by": "test-user",
    }
    create_response = client.post(
        "/api/v2/clean-release/candidates", json=bootstrap_candidate
    )
    assert create_response.status_code == 201
    payload = {
        "artifacts": [
            {"id": "art-1", "path": "bin/app.exe", "sha256": "hash123", "size": 1024}
        ]
    }
    response = client.post(
        f"/api/v2/clean-release/candidates/{candidate_id}/artifacts", json=payload
    )
    assert response.status_code == 200
    assert response.json()["status"] == "success"
# #endregion test_artifact_import_contract
# #region test_manifest_build_contract [TYPE Function]
# @RELATION BINDS_TO -> CleanReleaseV2ApiTests
# @BRIEF Validate manifest build endpoint produces manifest payload linked to the target candidate.
def test_manifest_build_contract():
    """
    @TEST_SCENARIO: manifest_build -> Should return 201 and manifest DTO.
    @TEST_CONTRACT: POST /api/v2/clean-release/candidates/{id}/manifests -> ManifestDTO
    """
    candidate_id = "rc-test-001-manifest"
    bootstrap_candidate = {
        "id": candidate_id,
        "version": "1.0.0",
        "source_snapshot_ref": "git:sha123",
        "created_by": "test-user",
    }
    create_response = client.post(
        "/api/v2/clean-release/candidates", json=bootstrap_candidate
    )
    assert create_response.status_code == 201
    response = client.post(f"/api/v2/clean-release/candidates/{candidate_id}/manifests")
    assert response.status_code == 201
    data = response.json()
    assert "manifest_digest" in data
    assert data["candidate_id"] == candidate_id
# #endregion test_manifest_build_contract
# #endregion CleanReleaseV2ApiTests
