"""Test authentication utilities for getting JWT tokens."""

import json
import os
from typing import Optional


def get_test_token(user_email: str = "test@example.com") -> Optional[str]:
    """Get a test JWT token for the specified user.

    Args:
        user_email: Email address to get token for

    Returns:
        JWT token string if available, None otherwise
    """
    try:
        # Only use tokens if JWT auth is enabled
        if os.getenv("ENABLE_JWT_AUTH", "true").lower() == "true":
            # Read tokens from file generated by server
            test_tokens_file = "test_tokens.json"
            if os.path.exists(test_tokens_file):
                with open(test_tokens_file, "r") as f:
                    test_tokens = json.load(f)
                return test_tokens.get(user_email)
            else:
                print(
                    f"Warning: Test tokens file {test_tokens_file} not found. Make sure server is running."
                )
                return None
        return None
    except Exception as e:
        print(f"Error getting test token: {e}")
        return None


def get_client_auth_config(user_email: str = "test@example.com") -> Optional[str]:
    """Get authentication configuration for FastMCP client.

    Args:
        user_email: Email address to get token for

    Returns:
        JWT token for Bearer auth if JWT enabled, None otherwise
    """
    return get_test_token(user_email)
