# ========================================================================
# Get User Details 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 USER DETAILS API ENDPOINT")

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

class API_Request(API_BaseRequest):
    username: str = ""

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


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

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

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

    if(request.username == ""):
        api_response.success = False
        api_response.status_code = 404
        api_response.reason = "Username Missing"
        return api_response

    # Get all user details
    request_details = {}

    user_attributes = user.get_attributes(request.username)
    profile_details = {}
    profile_details['username'] = request.username
    profile_details['fname'] = user_attributes.get('fname')
    profile_details['lname'] = user_attributes.get('lname')
    profile_details['avatar'] = user_attributes.get('avatar')
    profile_details['account_type'] = user_attributes.get('account_type')
    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')
    profile_details['skills'] = user_attributes.get('skills')

    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
