from ninja import NinjaAPI
from ninja.security import django_auth
from submissions.api import router as submissions_router
from submissions.schemas import UserInfoOut

# Create the main API instance
api = NinjaAPI(
    title="Opus Magnum Submission API",
    version="1.0.0",
    description="API for managing Opus Magnum puzzle submissions",
)

# Add authentication for protected endpoints
# api.auth = django_auth  # Uncomment if you want global auth

# Include the submissions router
api.add_router("/submissions/", submissions_router, tags=["submissions"])


# Health check endpoint
@api.get("/health")
def health_check(request):
    """Health check endpoint"""
    return {"status": "healthy", "service": "opus-magnum-api"}


# API info endpoint
@api.get("/info")
def api_info(request):
    """Get API information"""
    return {
        "name": "Opus Magnum Submission API",
        "version": "1.0.0",
        "description": "API for managing puzzle submissions with OCR validation",
        "features": [
            "Multi-puzzle submissions",
            "File upload to S3",
            "OCR validation tracking",
            "Manual validation workflow",
            "Admin validation tools",
        ],
    }


# User info endpoint
@api.get("/user", response=UserInfoOut)
def get_user_info(request):
    """Get current user information"""
    user = request.user
    
    if user.is_authenticated:
        return {
            "id": user.id,
            "username": user.username,
            "first_name": user.first_name,
            "last_name": user.last_name,
            "email": user.email,
            "is_authenticated": True,
            "is_staff": user.is_staff,
            "is_superuser": user.is_superuser,
            "cas_groups": getattr(user, 'cas_groups', [])
        }
    else:
        return {
            "is_authenticated": False,
            "is_staff": False,
            "is_superuser": False,
        }
