# ========================================================================
# User ID API Endpoint
#

from fastapi import APIRouter
from api.core.EasyMessage import API_BaseRequest
from api.core.EasyMessage import API_BaseResponse
from api.core.EasySQL import EasySQL
from api.core.EasyUser import EasyUser

import json

print("GET PROFIEL DETAILS API ENDPOINT")

router = APIRouter()
sql_connection = EasySQL()
user = EasyUser()

class API_Request(API_BaseRequest):
    pass

class API_Response(API_BaseResponse):
    output: str = "{}"


@router.post("/users/get_profile_details")
def update_name(request: API_Request):

    # Default Session Check
    api_response = API_Response()
    session_hash = request.session_hash
    username = user.authenticate_session(session_hash)

    if(username == False):
        api_response.success = False
        api_response.status_code = 403
        api_response.reason = "Login Failure"
        return api_response

    # Get all user details
    profile_details = {}

    user_attributes = user.get_attributes(username)

    profile_details['username'] = username
    profile_details['fname'] = user_attributes.get('fname')
    profile_details['lname'] = user_attributes.get('lname')
    profile_details['post_code'] = user_attributes.get('post_code')
    profile_details['email'] = user_attributes.get('email')
    profile_details['avatar'] = user_attributes.get('avatar')
    profile_details['account_type'] = user_attributes.get('account_type')
    profile_details['school_name'] = user_attributes.get('school_name')
    profile_details['school_location'] = user_attributes.get('school_location')
    profile_details['driving_license'] = user_attributes.get('driving_license')
    profile_details['company_name'] = user_attributes.get('company_name')
    profile_details['company_location'] = user_attributes.get('company_location')
    profile_details['employers_insurance'] = user_attributes.get('employers_insurance')

    if(profile_details.get('avatar') == None):
        profile_details['avatar'] = 'default'

    api_response.success = True
    api_response.status_code = 200
    api_response.reason = ""
    api_response.output = json.dumps(profile_details)
    return api_response
