from flask import Blueprint, Response, request
from app.core.response import build_response
from app.core.security import require_jwt
from app.services.hub_services.login import generate_hub_validation_code_service, validate_hub_login_service, logout_hub_user_service
from app.services.hub_services.tokens import refresh_access_token_service


hub_login_routes = Blueprint('hub_login_routes', __name__)


# Generates the validation code used to login
@hub_login_routes.route('/generate_hub_validation_code/<int:id>/',
                        methods=['GET'])
def generate_hub_validation_code(id: int) -> Response:
    response, status_codes = generate_hub_validation_code_service(id=id)
    return build_response(
        data=response,
        status_codes=status_codes
    )


# Checks if the code the user entered is correct and logs them in
@hub_login_routes.route('/validate_hub_login/<int:id>/', methods=['POST'])
def validate_hub_login(id: int) -> Response:
    data = request.json

    response, status_codes, tokens = validate_hub_login_service(
        submitted_code=data, id=id
    )

    # If the tokens were successfully generated, we send them directly through
    # the responses body
    if tokens:
        response["access_token"] = tokens.get("access_token")
        response["refresh_token"] = tokens.get("refresh_token")

    return build_response(
        data=response,
        status_codes=status_codes
    )


# Refreshes the Access token if the Refresh token is still valid
@hub_login_routes.route('/refresh_access_token/', methods=['GET'])
def refresh_access_token() -> Response:
    # Read refresh token from Authorization header
    auth_header = request.headers.get('Authorization')
    if not auth_header or not auth_header.startswith("Bearer "):
        return build_response(
            error='Refresh token nije prosleđen',
            status_codes=[401])

    refresh_token = auth_header.split(" ")[1]

    response, status_codes, tokens = refresh_access_token_service(
        refresh_token=refresh_token)

    # If tokens were generated, we return them inside body
    if tokens:
        response["access_token"] = tokens.get("access_token")
        response["refresh_token"] = tokens.get("refresh_token")

    return build_response(
        data=response,
        status_codes=status_codes
    )


# Removes the refresh token
@hub_login_routes.route('/logout_hub_user/<int:id>/', methods=['POST'])
@require_jwt(match_id_from_token='id')
def logout_hub_user(id: int) -> Response:
    # Read refresh token from Authorization header
    auth_header = request.headers.get('Authorization')
    if not auth_header or not auth_header.startswith("Bearer "):
        return build_response(
            error='Refresh token nije prosleđen',
            status_codes=[401])

    refresh_token = auth_header.split(" ")[1]

    response, status_codes, _ = logout_hub_user_service(
        refresh_token=refresh_token)

    return build_response(
        data=response,
        status_codes=status_codes
    )
