from fastapi import APIRouter, HTTPException, status, Depends
from fastapi.responses import RedirectResponse
from sqlalchemy.orm import Session
from slowapi import Limiter
from slowapi.util import get_remote_address
from ..database import get_db
from ..models.user import User
from ..models.twitch_oauth import TwitchOAuth
from ..schemas.twitch import TwitchOAuthRequest, TwitchOAuthResponse, TwitchUserInfo
from ..auth import get_current_active_user
from ..config import settings

router = APIRouter(prefix="/api/auth/twitch", tags=["twitch-auth"])
limiter = Limiter(key_func=get_remote_address)


@router.get("/login")
async def twitch_login(
    current_user: User = Depends(get_current_active_user)
):
    """
    Redirect to Twitch OAuth login page (for future implementation)
    This endpoint is prepared but will return 503 until Twitch credentials are configured
    """
    raise HTTPException(
        status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
        detail="Twitch OAuth is not configured yet. Please set TWITCH_CLIENT_ID and TWITCH_CLIENT_SECRET in environment variables."
    )


@router.get("/callback")
async def twitch_callback(
    code: str,
    state: str = None,
    db: Session = Depends(get_db)
):
    """
    Handle Twitch OAuth callback (for future implementation)
    This endpoint is prepared but will return 503 until Twitch credentials are configured
    """
    raise HTTPException(
        status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
        detail="Twitch OAuth is not configured yet. Please set TWITCH_CLIENT_ID and TWITCH_CLIENT_SECRET in environment variables."
    )


@router.post("/unlink", status_code=status.HTTP_204_NO_CONTENT)
async def unlink_twitch(
    db: Session = Depends(get_db),
    current_user: User = Depends(get_current_active_user)
):
    """
    Unlink Twitch account from user account (for future implementation)
    This endpoint is prepared but will return 503 until Twitch credentials are configured
    """
    raise HTTPException(
        status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
        detail="Twitch OAuth is not configured yet. Please set TWITCH_CLIENT_ID and TWITCH_CLIENT_SECRET in environment variables."
    )
