# [DEF:test_clean_release_v2_api:Module]
# @TIER: STANDARD
# @PURPOSE: API contract tests for redesigned clean release endpoints.
# @LAYER: Domain

from datetime import datetime, timezone
from types import SimpleNamespace
from uuid import uuid4

import pytest
from fastapi.testclient import TestClient

from src.app import app
from src.dependencies import get_clean_release_repository, get_config_manager
from src.models.clean_release import (
    CleanPolicySnapshot,
    DistributionManifest,
    ReleaseCandidate,
    SourceRegistrySnapshot,
)
from src.services.clean_release.enums import CandidateStatus

client = TestClient(app)

# [REASON] Implementing API contract tests for candidate/artifact/manifest endpoints (T012).
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

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"

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

# [/DEF:test_clean_release_v2_api:Module]