"""Tests for PATCH /auth/me profile update endpoint."""

import pytest
from httpx import AsyncClient


@pytest.mark.asyncio
class TestProfileUpdate:
    """Test profile update via PATCH /auth/me."""

    async def test_update_name(self, client: AsyncClient, auth_headers: dict):
        """Name update works without password."""
        response = await client.patch(
            "/api/v1/auth/me",
            json={"name": "New Name"},
            headers=auth_headers,
        )
        assert response.status_code == 200
        assert response.json()["name"] == "New Name"

    async def test_update_email_with_password(self, client: AsyncClient, auth_headers: dict):
        """Email change with correct password succeeds."""
        response = await client.patch(
            "/api/v1/auth/me",
            json={"email": "newemail@example.com", "current_password": "TestPassword123!"},
            headers=auth_headers,
        )
        assert response.status_code == 200
        assert response.json()["email"] == "newemail@example.com"

    async def test_update_email_without_password(self, client: AsyncClient, auth_headers: dict):
        """Email change without password returns 400."""
        response = await client.patch(
            "/api/v1/auth/me",
            json={"email": "newemail@example.com"},
            headers=auth_headers,
        )
        assert response.status_code == 400
        assert "password" in response.json()["detail"].lower()

    async def test_update_email_wrong_password(self, client: AsyncClient, auth_headers: dict):
        """Email change with wrong password returns 401."""
        response = await client.patch(
            "/api/v1/auth/me",
            json={"email": "newemail@example.com", "current_password": "WrongPassword123!"},
            headers=auth_headers,
        )
        assert response.status_code == 401

    async def test_update_email_duplicate(self, client: AsyncClient, auth_headers: dict):
        """Email change to existing email returns 400."""
        # Register second user
        await client.post("/api/v1/auth/register", json={
            "email": "other@example.com",
            "password": "TestPassword123!",
            "name": "Other User",
        })

        response = await client.patch(
            "/api/v1/auth/me",
            json={"email": "other@example.com", "current_password": "TestPassword123!"},
            headers=auth_headers,
        )
        assert response.status_code == 400
        assert "already registered" in response.json()["detail"].lower()

    async def test_get_me_returns_updated_name(self, client: AsyncClient, auth_headers: dict):
        """GET /me reflects the updated profile."""
        await client.patch(
            "/api/v1/auth/me",
            json={"name": "Updated User"},
            headers=auth_headers,
        )
        response = await client.get("/api/v1/auth/me", headers=auth_headers)
        assert response.status_code == 200
        assert response.json()["name"] == "Updated User"

    async def test_no_changes_returns_current_user(self, client: AsyncClient, auth_headers: dict):
        """Empty update returns current user without error."""
        response = await client.patch(
            "/api/v1/auth/me",
            json={},
            headers=auth_headers,
        )
        assert response.status_code == 200

    async def test_unauthenticated(self, client: AsyncClient):
        """Unauthenticated request returns 401."""
        response = await client.patch("/api/v1/auth/me", json={"name": "X"})
        assert response.status_code == 401
