"""Tests for face registration and identification endpoints."""

import pytest
from httpx import AsyncClient


@pytest.mark.asyncio
async def test_face_register_new_user(client: AsyncClient, sample_jpeg: bytes):
    """POST /faces/register → creates new user when no match."""
    resp = await client.post(
        "/api/v1/faces/register",
        data={"user_name": "테스트 사용자"},
        files={"image": ("face.jpg", sample_jpeg, "image/jpeg")},
    )
    assert resp.status_code == 200
    data = resp.json()
    assert "user_id" in data
    assert data["confidence"] > 0
    # First registration may or may not be new depending on DB state
    assert isinstance(data["is_new_user"], bool)


@pytest.mark.asyncio
async def test_face_register_without_name(client: AsyncClient, sample_jpeg: bytes):
    """POST /faces/register without user_name → 422 (name is required)."""
    resp = await client.post(
        "/api/v1/faces/register",
        files={"image": ("face.jpg", sample_jpeg, "image/jpeg")},
    )
    assert resp.status_code == 422


@pytest.mark.asyncio
async def test_face_identify(client: AsyncClient, sample_jpeg: bytes):
    """POST /faces/identify → returns matched user or 404."""
    # Register first
    reg_resp = await client.post(
        "/api/v1/faces/register",
        data={"user_name": "ID Test"},
        files={"image": ("face.jpg", sample_jpeg, "image/jpeg")},
    )
    assert reg_resp.status_code == 200

    # Identify with same image (mock returns same embedding)
    resp = await client.post(
        "/api/v1/faces/identify",
        files={"image": ("face.jpg", sample_jpeg, "image/jpeg")},
    )
    # Should find a match since we just registered with the same embedding
    assert resp.status_code in (200, 404)
    if resp.status_code == 200:
        data = resp.json()
        assert "user_id" in data
        assert data["confidence"] > 0


@pytest.mark.asyncio
async def test_face_register_missing_image(client: AsyncClient):
    """POST /faces/register without image → 422 validation error."""
    resp = await client.post("/api/v1/faces/register")
    assert resp.status_code == 422
