"""
Tests for authentication APIs.
"""
import pytest
from fastapi import status


class TestSignUp:
    """Test sign up functionality."""
    
    def test_signup_with_valid_data(self, client, test_user_data):
        """Test creating new user with valid data."""
        response = client.post(
            "/api/v1/auth/signup",
            json=test_user_data
        )
        
        assert response.status_code in [status.HTTP_200_OK, status.HTTP_201_CREATED]
        data = response.json()
        assert "token" in data or "user" in data
        assert "email" in data.get("user", {}) or "email" in data
    
    def test_signup_duplicate_email(self, client, test_user_data):
        """Test rejecting duplicate email."""
        # First signup
        client.post("/api/v1/auth/signup", json=test_user_data)
        
        # Try to signup again with same email
        response = client.post(
            "/api/v1/auth/signup",
            json=test_user_data
        )
        
        assert response.status_code == status.HTTP_400_BAD_REQUEST
    
    def test_signup_missing_fields(self, client):
        """Test signup with missing required fields."""
        response = client.post(
            "/api/v1/auth/signup",
            json={"email": "test@example.com"}  # Missing password
        )
        
        assert response.status_code == status.HTTP_400_BAD_REQUEST


class TestSignIn:
    """Test sign in functionality."""
    
    def test_signin_with_valid_credentials(self, client, test_user_data):
        """Test signing in with valid credentials."""
        # First create user
        client.post("/api/v1/auth/signup", json=test_user_data)
        
        # Sign in
        response = client.post(
            "/api/v1/auth/signin",
            json={
                "email": test_user_data["email"],
                "password": test_user_data["password"],
            }
        )
        
        assert response.status_code == status.HTTP_200_OK
        data = response.json()
        assert "token" in data or "user" in data
    
    def test_signin_invalid_credentials(self, client, test_user_data):
        """Test rejecting invalid credentials."""
        response = client.post(
            "/api/v1/auth/signin",
            json={
                "email": test_user_data["email"],
                "password": "wrongpassword",
            }
        )
        
        assert response.status_code == status.HTTP_400_BAD_REQUEST


class TestGetCurrentUser:
    """Test get current user functionality."""
    
    def test_get_me_authenticated(self, client, authenticated_headers):
        """Test getting user info when authenticated."""
        response = client.get(
            "/api/v1/auth/me",
            headers=authenticated_headers
        )
        
        if authenticated_headers:
            assert response.status_code == status.HTTP_200_OK
            data = response.json()
            assert "user" in data
        else:
            # If no auth token, should return 401
            assert response.status_code == status.HTTP_401_UNAUTHORIZED
    
    def test_get_me_unauthenticated(self, client):
        """Test getting user info when not authenticated."""
        response = client.get("/api/v1/auth/me")
        
        assert response.status_code == status.HTTP_401_UNAUTHORIZED

