"""Integration tests for email/phone verification"""

import uuid

import pytest
import pytest_asyncio


@pytest_asyncio.fixture
async def auth_ctx(client):
    """Register a user and return headers + email"""
    email = f"t{uuid.uuid4().hex[:6]}@test.cn"
    resp = await client.post("/api/v1/auth/register", json={
        "email": email, "password": "Test1234", "display_name": "TestDoctor",
    })
    assert resp.status_code == 200
    data = resp.json()
    return {"Authorization": f"Bearer {data['token']['access_token']}"}, email


@pytest.mark.asyncio
async def test_send_email_code(client, auth_ctx):
    """POST /auth/verification/send-email returns sent status"""
    headers, email = auth_ctx
    resp = await client.post("/api/v1/auth/verification/send-email",
                             json={"email": email},
                             headers=headers,
                             timeout=30)
    assert resp.status_code == 200
    data = resp.json()
    assert data["status"] == "sent"


@pytest.mark.asyncio
async def test_send_email_code_wrong_email(client, auth_ctx):
    """Sending to email different from registered email returns 400"""
    headers, _ = auth_ctx
    resp = await client.post("/api/v1/auth/verification/send-email",
                             json={"email": "wrong@test.cn"},
                             headers=headers)
    assert resp.status_code == 400


@pytest.mark.asyncio
async def test_verify_email_wrong_code(client, auth_ctx):
    """Wrong code returns 400"""
    headers, _ = auth_ctx
    resp = await client.post("/api/v1/auth/verification/verify-email",
                             json={"code": "000000"},
                             headers=headers)
    assert resp.status_code == 400


@pytest.mark.asyncio
async def test_send_email_code_requires_auth(client):
    """No auth returns 401"""
    resp = await client.post("/api/v1/auth/verification/send-email",
                             json={"email": "test@test.cn"})
    assert resp.status_code == 401


@pytest.mark.asyncio
async def test_send_phone_code(client, auth_ctx):
    """POST /auth/verification/send-phone returns sent status"""
    headers, _ = auth_ctx
    resp = await client.post("/api/v1/auth/verification/send-phone",
                             json={"phone": "13800138000"},
                             headers=headers)
    assert resp.status_code == 200
    data = resp.json()
    assert data["status"] == "sent"


@pytest.mark.asyncio
async def test_send_phone_code_rate_limit(client, auth_ctx):
    """Rapid resend returns 429"""
    headers, _ = auth_ctx
    resp1 = await client.post("/api/v1/auth/verification/send-phone",
                              json={"phone": "13800138001"},
                              headers=headers)
    assert resp1.status_code == 200

    resp2 = await client.post("/api/v1/auth/verification/send-phone",
                              json={"phone": "13800138001"},
                              headers=headers)
    assert resp2.status_code == 429


@pytest.mark.asyncio
async def test_verify_phone_wrong_code(client, auth_ctx):
    """Wrong phone code returns 400"""
    headers, _ = auth_ctx
    resp = await client.post("/api/v1/auth/verification/verify-phone",
                             json={"code": "000000"},
                             headers=headers)
    assert resp.status_code == 400
