"""Smoke tests exécutables après déploiement du backend admin."""

from __future__ import annotations

import os
from typing import Iterator

import httpx
import pytest

BASE_URL = os.getenv("ADMIN_API_BASE_URL", "http://localhost:8000")
ADMIN_EMAIL = os.getenv("ADMIN_EMAIL", "sysop@web-sentinel.com")
ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD", "secret")


@pytest.fixture(scope="module")
def client() -> Iterator[httpx.Client]:
    with httpx.Client(base_url=BASE_URL, timeout=5.0) as http_client:
        yield http_client


def _require_api(client: httpx.Client) -> None:
    try:
        response = client.get("/health")
    except httpx.HTTPError:
        pytest.skip("API admin indisponible pour le test de fumée")
    if response.status_code != 200:
        pytest.skip("API admin indisponible pour le test de fumée")


def test_healthcheck_ok(client: httpx.Client) -> None:
    _require_api(client)
    response = client.get("/health")
    assert response.status_code == 200
    assert response.json()["status"] == "ok"


def test_login_and_dashboard_smoke(client: httpx.Client) -> None:
    _require_api(client)
    response = client.post(
        "/api/v1/auth/login",
        data={"username": ADMIN_EMAIL, "password": ADMIN_PASSWORD},
        headers={"Content-Type": "application/x-www-form-urlencoded"},
    )
    assert response.status_code == 200
    token = response.json()["access_token"]

    dashboard = client.get(
        "/api/v1/dashboard/summary",
        headers={"Authorization": f"Bearer {token}"},
    )
    assert dashboard.status_code == 200
