"""
API 集成测试

测试认证 API 的集成流程。
需要 PostgreSQL 数据库才能运行。在没有数据库的环境中会被跳过。
"""

import os
import uuid

import pytest

# 检测是否有可用的 PostgreSQL 数据库
_HAS_PG = False
try:
    if os.environ.get("USE_IN_MEMORY_DB", "").lower() != "true":
        import psycopg

        conn = psycopg.connect(
            os.environ.get(
                "DATABASE_URL",
                "postgresql+psycopg://postgres:postgres@localhost:5432/xiaoxia_saas",
            ).replace("postgresql+psycopg://", "postgresql://"),
            connect_timeout=3,
        )
        conn.close()
        _HAS_PG = True
except Exception:
    pass

needs_pg = pytest.mark.skipif(not _HAS_PG, reason="Requires PostgreSQL database")

from fastapi.testclient import TestClient

from apps.api.main import app

client = TestClient(app)


@needs_pg
class TestAuthAPI:
    """认证 API 集成测试"""

    def test_register_success(self):
        """测试注册成功"""
        unique = uuid.uuid4().hex[:8]
        response = client.post(
            "/api/v1/auth/register",
            json={
                "email": f"test-{unique}@example.com",
                "password": "SecurePass123",
                "username": f"testuser-{unique}",
                "display_name": "Test User",
            },
        )

        assert response.status_code in (200, 201)
        data = response.json()
        assert data["username"] == f"testuser-{unique}"
        assert "user_id" in data

    def test_register_duplicate_email(self):
        """测试重复邮箱注册"""
        unique = uuid.uuid4().hex[:8]
        email = f"dup-{unique}@example.com"

        client.post(
            "/api/v1/auth/register",
            json={
                "email": email,
                "password": "SecurePass123",
                "username": f"user1-{unique}",
                "display_name": "User 1",
            },
        )

        response = client.post(
            "/api/v1/auth/register",
            json={
                "email": email,
                "password": "SecurePass123",
                "username": f"user2-{unique}",
                "display_name": "User 2",
            },
        )

        assert response.status_code == 400
        body = response.json()
        message = body.get("detail", "") or body.get("error", {}).get("message", "")
        assert "邮箱" in message or "already" in message.lower() or "注册" in message

    def test_login_success(self):
        """测试登录成功"""
        unique = uuid.uuid4().hex[:8]
        email = f"login-{unique}@example.com"

        reg = client.post(
            "/api/v1/auth/register",
            json={
                "email": email,
                "password": "SecurePass123",
                "username": f"loginuser-{unique}",
                "display_name": "Login User",
            },
        )
        assert reg.status_code in (200, 201), f"Register failed: {reg.json()}"

        response = client.post(
            "/api/v1/auth/login",
            json={
                "email": email,
                "password": "SecurePass123",
            },
        )

        assert response.status_code == 200
        data = response.json()
        assert "access_token" in data
        assert "refresh_token" in data
        assert data["token_type"] == "bearer"

    def test_login_wrong_password(self):
        """测试密码错误"""
        response = client.post(
            "/api/v1/auth/login",
            json={
                "email": "nobody@example.com",
                "password": "WrongPassword123",
            },
        )

        assert response.status_code == 401


if __name__ == "__main__":
    pytest.main([__file__, "-v"])
