import os

from fastapi import APIRouter, Body, HTTPException, Request
from fastapi.responses import JSONResponse, RedirectResponse

from configs.config import ACCESS_TOKEN, ID_TOKEN, REFRESH_TOKEN
from models.user import UserBase
from services.auth_service import AuthService
from utils.dynamo_db import DynamoDB

auth = APIRouter(prefix="/auth", tags=["Authentication"])


class UserInfo(UserBase):
    """
    User information model extending UserBase for authentication responses
    """

    pass


auth_service = AuthService()


@auth.get("/login")
async def login():
    """
    Redirects to Cognito-hosted login page.
    """
    login_url = await auth_service.login()
    return RedirectResponse(login_url)


@auth.post("/callback")
async def callback(body=Body(...)):
    """
    Handles Cognito's callback with the authorization code and redirects to frontend.
    """
    print(body)
    try:
        result = await auth_service.handle_callback(body.get("code"))
        response = JSONResponse(
            status_code=200,
            content={
                "message": "Authentication successful",
                "redirect_url": result["frontend_url"],
            },
        )

        is_local = os.environ.get("AWS_SAM_LOCAL") == "true"
        response.set_cookie(
            key=REFRESH_TOKEN,
            value=result["refresh_token"],
            httponly=True,
            secure=not is_local,
            max_age=3600,
        )

        return response
    except Exception as e:
        print(e)
        raise HTTPException(status_code=500, detail="Failed to handle callback")


@auth.get("/user")
async def get_user_info(request: Request):
    current_user = request.state.current_user

    if current_user:
        db = DynamoDB()

        user_details = db.get_item(db.user, current_user.email)
        if user_details:
            return UserInfo(**user_details)

        user_details = db.get_item(db.user, current_user.user_id)
        if user_details:
            return UserInfo(**user_details)

    raise HTTPException(status_code=404, detail="User not found")


@auth.post("/logout")
async def logout():
    """Handle user logout"""
    logout_url = auth_service.get_logout_url()
    response = JSONResponse(status_code=200, content={"logout_url": logout_url})
    response.delete_cookie(REFRESH_TOKEN)
    return response


@auth.post("/refresh-token")
async def refresh_token(request: Request):
    """
    Refresh the access token using the refresh token stored in cookies
    """
    refresh_token = request.cookies.get(REFRESH_TOKEN)
    tokens = await auth_service.refresh_token(refresh_token)

    response = JSONResponse(
        status_code=200,
        content={
            "access_token": tokens.get(ACCESS_TOKEN),
            "id_token": tokens.get(ID_TOKEN),
            "expires_in": tokens.get("expires_in"),
        },
    )

    new_refresh_token = tokens.get(REFRESH_TOKEN) or refresh_token
    response.set_cookie(
        key=REFRESH_TOKEN,
        value=new_refresh_token,
        httponly=True,
        secure=False,
        max_age=3600,
        path="/",
        samesite="lax",
    )

    return response
