"""
Test script for token-based authentication in Dynamic Highscores.

This script tests the token-based authentication functionality.
"""

import os
import sys
import requests
from huggingface_hub import HfApi

def test_token_auth():
    """Test token-based authentication with HuggingFace API."""
    
    print("Testing token-based authentication...")
    
    # Prompt for tokens
    read_token = input("Enter your HuggingFace read token: ")
    write_token = input("Enter your HuggingFace write token: ")
    
    if not read_token or not write_token:
        print("Error: Both read and write tokens are required.")
        return False
    
    # Initialize HuggingFace API
    hf_api = HfApi()
    
    # Test read token
    print("\nTesting read token...")
    try:
        read_user_info = hf_api.whoami(token=read_token)
        print(f"Read token valid. User: {read_user_info.get('name')}")
    except Exception as e:
        print(f"Error validating read token: {e}")
        return False
    
    # Test write token
    print("\nTesting write token...")
    try:
        write_user_info = hf_api.whoami(token=write_token)
        print(f"Write token valid. User: {write_user_info.get('name')}")
    except Exception as e:
        print(f"Error validating write token: {e}")
        return False
    
    # Check if tokens belong to the same user
    if read_user_info.get("id") != write_user_info.get("id"):
        print("Error: Tokens must belong to the same user.")
        return False
    
    print("\nToken authentication test successful!")
    print(f"User: {read_user_info.get('name')}")
    print(f"User ID: {read_user_info.get('id')}")
    
    return True

if __name__ == "__main__":
    test_token_auth()