from __future__ import annotations

from pathlib import Path

from fastapi import FastAPI
from fastapi.testclient import TestClient

from src.api.v1.agents import router
from src.services.product_awoooi_manifest_standard import (
    load_latest_product_awoooi_manifest_standard,
)


def test_product_awoooi_manifest_standard_loader_reports_ready_manifest():
    payload = load_latest_product_awoooi_manifest_standard()

    assert payload["schema_version"] == "product_awoooi_manifest_standard_readiness_v1"
    assert payload["status"] == "ready_for_product_manifest_adoption"
    assert payload["priority"] == "P0-002"
    assert payload["readback"]["workplan_id"] == "P0-002"
    assert payload["manifest"]["present"] is True
    assert payload["manifest"]["schema_present"] is True
    assert payload["manifest"]["schema_version"] == "product_awoooi_manifest_v1"
    assert payload["manifest"]["product_id"] == "awoooi"
    assert payload["manifest"]["source_control_authority"] == "gitea"
    assert payload["manifest"]["github_status"] == "stopped_retired_do_not_use"
    assert payload["rollups"]["source_readiness_percent"] == 100
    assert payload["rollups"]["missing_required_section_count"] == 0
    assert payload["rollups"]["missing_contract_ref_count"] == 0
    assert payload["operation_boundaries"]["read_only_api_allowed"] is True
    assert payload["operation_boundaries"]["remaining_product_repo_write_allowed"] is False
    assert payload["operation_boundaries"]["github_api_allowed"] is False
    assert payload["operation_boundaries"]["secret_read_allowed"] is False


def test_product_awoooi_manifest_standard_endpoint_returns_snapshot():
    app = FastAPI()
    app.include_router(router, prefix="/api/v1")
    client = TestClient(app)

    response = client.get("/api/v1/agents/product-awoooi-manifest-standard")

    assert response.status_code == 200
    data = response.json()
    assert data["schema_version"] == "product_awoooi_manifest_standard_readiness_v1"
    assert data["status"] == "ready_for_product_manifest_adoption"
    assert data["rollups"]["source_readiness_percent"] == 100
    assert data["operation_boundaries"]["repo_creation_allowed"] is False


def test_api_image_copies_product_manifest() -> None:
    dockerfile = Path(__file__).resolve().parents[3] / "apps" / "api" / "Dockerfile"

    assert (
        "COPY --chown=appuser:appuser product.awoooi.yaml ./product.awoooi.yaml"
        in dockerfile.read_text(encoding="utf-8")
    )
