from fastapi import Depends from core import logging as logger from core.models.apis_models import * from fastapi.routing import APIRouter from core.api.jwt_bearer import access_check_bearer, supabase_client from core.pipeline.user_management_pipeline import SupabaseUserManagementPipeline from core.utils.error_handling import raise_http_exception, create_success_response, create_error_response user_management_api_router = APIRouter(tags=["User Management"]) user_management = SupabaseUserManagementPipeline() @user_management_api_router.post("/user_signup") async def user_signup(request: UserSignupRequest): logger.info(f">>>user_signup API Triggered <<<") response = user_management.user_signup_(username=request.username, password=request.password,email=request.email) logger.info(f">>>user_signup API Success<<<") return response @user_management_api_router.post("/user_signin") async def user_signin(request: UserSigninRequest): logger.info(f">>>user_signin API Triggered <<<") response = user_management.user_signin_(username=request.username, password=request.password,email=request.email) if response != None: logger.info(f">>>user_signin API Success.<<<") return response else: logger.info(f">>> Username or password is incorrect please try again.<<<") response = create_error_response(400, "Username or password is incorrect please try again.") return response @user_management_api_router.post("/get_user_data") async def get_user_data(request: GetUserDataRequest): logger.info(f">>>get_user_data API Triggered <<<") response = user_management.get_user_data_(access_token=request.access_token) return response @user_management_api_router.post("/login_with_access_token") async def login_with_access_token(request: LoginWithAccessTokenRequest): logger.info(f">>>login_with_access_token API Triggered <<<") response = user_management.login_with_access_token_(access_token=request.access_token, refresh_token=request.refresh_token) logger.info(f">>>login_with_access_token API Success<<<") return response @user_management_api_router.post("/set_session_data") async def set_session_data(request: SetSessionDataRequest): logger.info(f">>> set_session_data API Triggered <<<") response = user_management.set_session_data_(access_token=request.access_token, refresh_token=request.refresh_token, user_id=request.user_id) return response @user_management_api_router.post("/sign_out") async def sign_out(): logger.info(f">>> sign_out API Triggered <<<") response = user_management.sign_out_() logger.info(f">>>sign_out API Success<<<") return response @user_management_api_router.post("/oauth_signin") async def oauth_signin(): logger.info(f">>> oauth_signin API Triggered <<<") response = user_management.oauth_signin_() logger.info(f">>>oauth_signin API Success<<<") return response @user_management_api_router.post("/check_session") async def check_session(): logger.info(f">>>check_session API Triggered <<<") response = user_management.check_session_() return response @user_management_api_router.get("/get_public_chatbot") async def get_public_chatbots(): logger.info(f">>>get_public_chatbot API Triggered<<<") try: response = supabase_client.table("ChatAI_ChatbotInfo").select("*").eq("isPrivate", False).execute().data logger.info(f">>>Public chatbots fetched successfully.<<<") return response except Exception as e: logger.error(f">>>Error in get_public_chatbot: {e}<<<") raise_http_exception(500, "Internal Server Error") @user_management_api_router.post("/public_private_check") async def public_or_private(request: PublicPrivateCheckRequest): vectorstore, mode = request.vectorstore, request.mode logger.info(f">>>public_private_check API Triggered for {vectorstore}.<<<") try: username, chatbot_name = vectorstore.split("$")[1], vectorstore.split("$")[2] if len(mode) == 0: value = ( supabase_client.table("ChatAI_ChatbotInfo") .select("isPrivate") .eq("user_id", username) .eq("chatbotname", chatbot_name) .execute() ) value = value.data[0]["isPrivate"] response = create_success_response(200, {"output": value}) else: response = ( supabase_client.table("ChatAI_ChatbotInfo") .update({"isPrivate": mode}) .eq("user_id", username) .eq("chatbotname", chatbot_name) .execute() ) response = create_success_response(200, {"output": response}) logger.info(f">>>Public/Private check successful for {vectorstore}.<<<") return response except Exception as e: logger.error(f">>>Error in public_private_check: {e} for {vectorstore}.<<<") raise_http_exception(500, "Internal Server Error") @user_management_api_router.post("/refresh_session", dependencies=[Depends(access_check_bearer)]) async def refresh_session(request: RefreshSessionRequest): logger.info(f">>>refresh_session API Triggered <<<") response = user_management.refresh_session__(refresh_token=request.refresh_token) logger.info(f">>>refresh token fetched successfully.<<<") return response @user_management_api_router.post("/username_creation_oauth", dependencies=[Depends(access_check_bearer)]) async def username_creation_oauth(request: UsernameCreationOauthRequest): logger.info(f">>> username_creation_oauth API Triggered <<<") response = user_management.username_creation_oauth_(username=request.username,email=request.email, user_id=request.user_id) logger.info(f">>>username creation successful.<<<") return response