# #region TestReportsOpenapiConformance [TYPE Module] [C:3] [SEMANTICS tests, reports, openapi, conformance]
# @RELATION BINDS_TO -> SrcRoot
# @BRIEF Validate implemented reports payload shape against OpenAPI-required top-level contract fields.
# @LAYER Domain
# @INVARIANT  List and detail payloads include required contract keys.
from datetime import UTC, datetime
from types import SimpleNamespace

from fastapi.testclient import TestClient

from src.app import app
from src.core.task_manager.models import Task, TaskStatus
from src.dependencies import get_current_user, get_task_manager


# #region _FakeTaskManager [TYPE Class] [C:1]
# @RELATION BINDS_TO -> [TestReportsOpenapiConformance]
# @BRIEF Minimal task-manager fake exposing static task list for OpenAPI conformance checks.
# @INVARIANT  get_all_tasks returns seeded tasks unchanged.
class _FakeTaskManager:
    def __init__(self, tasks):
        self._tasks = tasks
    def get_all_tasks(self):
        return self._tasks
# #endregion _FakeTaskManager
# #region _admin_user [TYPE Function]
# @RELATION BINDS_TO -> TestReportsOpenapiConformance
# @BRIEF Provide admin principal fixture required by reports routes in conformance tests.
def _admin_user():
    role = SimpleNamespace(name="Admin", permissions=[])
    return SimpleNamespace(username="test-admin", roles=[role])
# #endregion _admin_user
# #region _task [TYPE Function]
# @RELATION BINDS_TO -> TestReportsOpenapiConformance
# @BRIEF Construct deterministic task fixture consumed by reports list/detail payload assertions.
def _task(task_id: str, plugin_id: str, status: TaskStatus):
    now = datetime.now(UTC)
    return Task(
        id=task_id,
        plugin_id=plugin_id,
        status=status,
        started_at=now,
        finished_at=now if status != TaskStatus.RUNNING else None,
        params={"environment_id": "env-1"},
        result={"summary": f"{plugin_id} {status.value.lower()}"},
    )
# #endregion _task
# #region test_reports_list_openapi_required_keys [TYPE Function]
# @RELATION BINDS_TO -> TestReportsOpenapiConformance
# @BRIEF Verify reports list endpoint includes all required OpenAPI top-level keys.
def test_reports_list_openapi_required_keys():
    tasks = [
        _task("r-1", "superset-backup", TaskStatus.SUCCESS),
        _task("r-2", "superset-migration", TaskStatus.FAILED),
    ]
    app.dependency_overrides[get_current_user] = lambda: _admin_user()
    app.dependency_overrides[get_task_manager] = lambda: _FakeTaskManager(tasks)
    try:
        client = TestClient(app)
        response = client.get("/api/reports")
        assert response.status_code == 200
        body = response.json()
        required = {
            "items",
            "total",
            "page",
            "page_size",
            "has_next",
            "applied_filters",
        }
        assert required.issubset(body.keys())
    finally:
        app.dependency_overrides.clear()
# #endregion test_reports_list_openapi_required_keys
# #region test_reports_detail_openapi_required_keys [TYPE Function]
# @RELATION BINDS_TO -> TestReportsOpenapiConformance
# @BRIEF Verify reports detail endpoint returns payload containing the report object key.
def test_reports_detail_openapi_required_keys():
    tasks = [_task("r-3", "llm_dashboard_validation", TaskStatus.SUCCESS)]
    app.dependency_overrides[get_current_user] = lambda: _admin_user()
    app.dependency_overrides[get_task_manager] = lambda: _FakeTaskManager(tasks)
    try:
        client = TestClient(app)
        response = client.get("/api/reports/r-3")
        assert response.status_code == 200
        body = response.json()
        assert "report" in body
    finally:
        app.dependency_overrides.clear()
# #endregion test_reports_detail_openapi_required_keys
# #endregion TestReportsOpenapiConformance
