"""Tests for signalement endpoints."""
import pytest
from httpx import AsyncClient

USER_PAYLOAD = {
    "email": "signalement_user@inphb.ci",
    "motDePasse": "TestPass123!",
    "nom": "Bamba",
    "prenom": "Moussa",
}

SIGNALEMENT_PAYLOAD = {
    "titre": "Test signalement",
    "description": "Description détaillée du signalement pour les tests",
    "type": "HARCELEMENT_MORAL",
    "modeIdentification": "ANONYME",
}


async def _get_token(client: AsyncClient) -> str:
    await client.post("/api/v1/auth/signup", json=USER_PAYLOAD)
    login = await client.post(
        "/api/v1/auth/login",
        json={"email": USER_PAYLOAD["email"], "motDePasse": USER_PAYLOAD["motDePasse"]},
    )
    return login.json()["data"]["token"]


@pytest.mark.asyncio
async def test_creer_signalement(client: AsyncClient):
    token = await _get_token(client)
    response = await client.post(
        "/api/v1/signalements/",
        json=SIGNALEMENT_PAYLOAD,
        headers={"Authorization": f"Bearer {token}"},
    )
    assert response.status_code == 201
    body = response.json()
    assert body["statusCode"] == 201
    data = body["data"]
    assert data["titre"] == SIGNALEMENT_PAYLOAD["titre"]
    assert data["statut"] == "NOUVEAU"
    assert data["reference"].startswith("SIG-")


@pytest.mark.asyncio
async def test_mes_signalements(client: AsyncClient):
    token = await _get_token(client)
    await client.post(
        "/api/v1/signalements/",
        json=SIGNALEMENT_PAYLOAD,
        headers={"Authorization": f"Bearer {token}"},
    )
    response = await client.get(
        "/api/v1/signalements/me",
        headers={"Authorization": f"Bearer {token}"},
    )
    assert response.status_code == 200
    body = response.json()
    assert body["data"]["total"] >= 1


@pytest.mark.asyncio
async def test_get_signalement_by_id(client: AsyncClient):
    token = await _get_token(client)
    create = await client.post(
        "/api/v1/signalements/",
        json=SIGNALEMENT_PAYLOAD,
        headers={"Authorization": f"Bearer {token}"},
    )
    signalement_id = create.json()["data"]["id"]

    response = await client.get(
        f"/api/v1/signalements/{signalement_id}",
        headers={"Authorization": f"Bearer {token}"},
    )
    assert response.status_code == 200
    assert response.json()["data"]["id"] == signalement_id


@pytest.mark.asyncio
async def test_non_traites_requires_gestionnaire(client: AsyncClient):
    token = await _get_token(client)  # regular UTILISATEUR
    response = await client.get(
        "/api/v1/signalements/non-traites",
        headers={"Authorization": f"Bearer {token}"},
    )
    assert response.status_code == 403
