import traceback
from flask import Blueprint, request
from dependency_injector.wiring import inject, Provide
import logging
import json
import bugsnag
import config
from emi_service import EMIService, validate_create_loan_request, validate_electricity_bill_request,validate_product_details_request
from emi_containers import EMIContainer
from response_templates.response_builders import (success_response, failure_response, custom_failure_response,
                                                  custom_failure_response_solar, success_response_solar)
from authentication.emi_auth import EMICheckoutMiddleware


emi_api = Blueprint('emi_api', __name__, url_prefix='/api')


@emi_api.route("/create-customer", methods=["POST"])
@EMICheckoutMiddleware.main
@inject
def create_customer(emi_service: EMIService = Provide[EMIContainer.emi_service]):
    try:
        logging.info(request.json)
        body = request.get_json(force=True)
        first_name = body['first_name']
        last_name = body['last_name']
        email = body['email']
        phone = body['phone']
        amount = body['loan_amount']
        merchant_id = body['merchant_id']
        center_id = body['center_id'] if 'center_id' in body else None
        product_id = body['product_id']
        gender = body['gender']
        pincode = body['pincode']
        result, code, data = emi_service.add_application(first_name, last_name, email, phone, merchant_id, center_id,
                                                         product_id, amount, gender, pincode)
        if result:
            logging.info('response: ' + json.dumps(data))
            return success_response(code=code, data=data, status_code=200)
        logging.info('response: De-dup check failed ' + json.dumps(data))
        if '409' in code:
            return custom_failure_response(status_code=409, code=code, application_id=data)
        return custom_failure_response(status_code=400, code=code, application_id=data)
    except Exception as ex:
        bugsnag.notify(ex)
        logging.exception(traceback.print_exc())
        return failure_response(status_code=400, message='Bad Request', code='CF10400')


@emi_api.route("/product-list", methods=["GET"], endpoint='product-list')
@EMICheckoutMiddleware.main
@inject
def get_product_list(emi_service: EMIService = Provide[EMIContainer.emi_service]):
    try:
        logging.info(request)
        merchant_id = request.args.get('merchant_id')
        result, code, result_list = emi_service.get_products(int(merchant_id))
        if result_list:
            logging.debug("returning success response")
            logging.info('response: ' + json.dumps(result_list))
            return success_response(status_code=200, code=code, data=result_list)
        return custom_failure_response(code=code, status_code=400)
    except Exception as ex:
        bugsnag.notify(ex)
        logging.exception(traceback.print_exc())
        return failure_response(status_code=400, code='CF10400')


@emi_api.route("/initiate-otp", methods=["POST"], endpoint='initiate-otp')
@EMICheckoutMiddleware.main
@inject
def generate_otp(emi_service: EMIService = Provide[EMIContainer.emi_service]):
    try:
        logging.info(request.json)
        body = request.get_json(force=True)
        mobile = body['phone']
        result, code, = emi_service.initiate_otp(mobile)
        if result:
            return success_response(status_code=200, code=code, message='OTP sent successfully')
        return custom_failure_response(status_code=400, code=code,)
    except Exception as ex:
        bugsnag.notify(ex)
        logging.exception(traceback.print_exc())
        return failure_response(status_code=400, code='CF10400')


@emi_api.route("/confirm-otp", methods=["POST"], endpoint='confirm-otp')
@EMICheckoutMiddleware.main
@inject
def verify_otp(emi_service: EMIService = Provide[EMIContainer.emi_service]):
    try:
        logging.info(request.json)
        body = request.get_json(force=True)
        mobile = body['phone']
        otp = body['otp']
        result, code, data = emi_service.confirm_otp(mobile, otp)
        if result:
            logging.info('response: ' + json.dumps(data))
            return success_response(status_code=200, code=code, data=data, message='OTP validate successfully')
        return custom_failure_response(status_code=400, code=code, )
    except Exception as ex:
        bugsnag.notify(ex)
        logging.exception(traceback.print_exc())
        return failure_response(status_code=400, code='CF10400')


@emi_api.route("/upload-document", methods=["POST"], endpoint='upload-document')
@EMICheckoutMiddleware.main
@inject
def upload_doc(emi_service: EMIService = Provide[EMIContainer.emi_service]):
    try:
        logging.info(request.json)
        body = request.get_json(force=True)
        cif = body['application_id']
        doc_name = body.get('docName')
        doc_password = body.get('docPassword')
        doc_content = body.get('docContent')
        doc_format = body.get('docFormat')
        bank_name = body.get('bank_name')
        account_type = body.get('account_type')
        user_id = body.get('user_id')
        result, code = emi_service.upload_document(cif, doc_name, doc_password, doc_content, doc_format, bank_name,
                                                   account_type, user_id)
        if result:
            return success_response(status_code=200, code=code, message='Document Uploaded',)
        if '409' in code:
            return custom_failure_response(status_code=409, code=code, )
        return custom_failure_response(status_code=400, code=code,)
    except Exception as ex:
        bugsnag.notify(ex)
        logging.exception(traceback.print_exc())
        return failure_response(status_code=400, code='CF10400')


@emi_api.route("/customer-details", methods=["POST"], endpoint='customer-details')
@EMICheckoutMiddleware.main
@inject
def customer_details(emi_service: EMIService = Provide[EMIContainer.emi_service]):
    try:
        body = request.get_json(force=True)
        raw_app_id = body['application_id']
        pan_number = body['pan_number']
        dob = body['dob']
        service_result, code = emi_service.update_customer_details(raw_app_id, pan_number, dob)
        if service_result:
            return success_response(status_code=200, code=code, message='Customer Details Updated')
        return custom_failure_response(status_code=400, code=code, )
    except Exception as ex:
        bugsnag.notify(ex)
        logging.exception(traceback.print_exc())
        return failure_response(status_code=400, code='CF10400')


@emi_api.route("/get-merchant-details", methods=["POST"], endpoint='get-merchant-details')
@EMICheckoutMiddleware.main
@inject
def get_merchant_details(emi_service: EMIService = Provide[EMIContainer.emi_service]):
    try:
        body = request.get_json(force=True)
        category_id = body.get('category_id')
        service_result, code, data = emi_service.display_merchants(category_id)
        merchants = {'merchants': data}
        if service_result:
            return success_response_solar(status_code=200, code=code, data=merchants, message='merchant lists')
        return custom_failure_response_solar(status_code=200, code=code, message='merchant fetch failed',statusCode="CF300")
    except Exception as ex:
        bugsnag.notify(ex)
        logging.exception(traceback.print_exc())
        return failure_response(status_code=400, code='CF10400')


@emi_api.route("/cancellation-request", methods=["GET"], endpoint='cancellation-request')
@EMICheckoutMiddleware.main
@inject
def cancellation_request(emi_service: EMIService = Provide[EMIContainer.emi_service]):
    try:
        logging.info(request)
        app_id = request.args.get('application_id')
        service_result, code = emi_service.application_cancellation_request(app_id)
        if service_result:
            return success_response(status_code=200, code=code, message='Cancellation Request is in Progress')
        return custom_failure_response(status_code=400, code=code, )
    except Exception as ex:
        bugsnag.notify(ex)
        logging.exception(traceback.print_exc())
        return failure_response(status_code=400, code='CF10400')


@emi_api.route("/get-status/<app_id>", methods=["GET"], endpoint='get-status/<app_id>')
@EMICheckoutMiddleware.main
@inject
def get_status(app_id, emi_service: EMIService = Provide[EMIContainer.emi_service]):
    try:
        logging.info(request)
        request_type = request.args.get('type')
        service_result, status, code = emi_service.get_application_status(app_id)
        if service_result:
            data = {'type': request_type, 'status': status}
            logging.info('response: ' + json.dumps(data))
            return success_response(status_code=200, code=code, data=data)
        return custom_failure_response(status_code=400, code=code, )
    except Exception as ex:
        bugsnag.notify(ex)
        logging.exception(traceback.print_exc())
        return failure_response(status_code=400, code='CF10400')


@emi_api.route("/digio_vkyc", methods=["POST"], endpoint='digio-kyc-api')
@EMICheckoutMiddleware.main
@inject
def digio_vkyc(emi_service: EMIService = Provide[EMIContainer.emi_service]):
    try:
        logging.info(request.json)
        body = request.get_json(force=True)
        raw_app_id = body['application_id']
        redirect_url = body['redirect_url'] if 'redirect_url' in body else None
        source = body.get("source")
        kyc_type = body.get("kyc_type")
        code, data = emi_service.digio_vkyc_method(raw_app_id, redirect_url, kyc_type)
        logging.info('response: ' + json.dumps(data))
        if data:
            if source == "no_touch":
                return success_response_solar(status_code=200, code="SF10200", data=data, message='merchant lists')
            return success_response(status_code=200, code=code, data=data)
        if source == "no_touch":
            return custom_failure_response_solar(status_code=200, code="CF10400", message="Fail", statusCode="CF300")
        return custom_failure_response(status_code=409, code=code, )

    except Exception as ex:
        bugsnag.notify(ex)
        logging.exception(traceback.print_exc())
        return failure_response(status_code=400, code='CF10400')


@emi_api.route("/digio_generate_token", methods=["POST"], endpoint='digio-generate-token')
@EMICheckoutMiddleware.main
@inject
def digio_generate_token(emi_service: EMIService = Provide[EMIContainer.emi_service]):
    try:
        logging.info(request.json)
        body = request.get_json(force=True)
        raw_app_id = body['application_id']
        app_type = 1 if 'applicant_type' not in body else body['applicant_type']
        code, data = emi_service.digio_generate_token(raw_app_id, app_type)
        logging.info('response: ' + json.dumps(data))
        if data:
            return success_response(status_code=200, code=code, data=data)
        return custom_failure_response(status_code=409, code=code, )

    except Exception as ex:
        bugsnag.notify(ex)
        logging.exception(traceback.print_exc())
        return failure_response(status_code=400, code='CF10400')


@emi_api.route("/ckyc", methods=["POST"], endpoint='ckyc')
@inject
def ckyc(emi_service: EMIService = Provide[EMIContainer.emi_service]):
    try:
        logging.info(request.json)
        body = request.get_json(force=True)
        pan_number = body.get('pan_number')
        dob = body.get('date_of_birth')
        mobile = body.get('mobile')
        merchant_id = body['merchant_id'] if body.get('merchant_id') else int(config.MNRE_MERCHANT)
        center_id = body.get('center_id')
        unique_id = body.get('unique_id')
        updated_by = 'Solar form via Merchant admin panel' if body.get('source') == 'map' else 'Partner Link'

        if merchant_id and mobile and dob and pan_number:
            code, response = emi_service.ckyc(pan_number=pan_number, dob=dob, mobile=mobile, merchant_id=merchant_id,
                                              center_id=center_id, unique_id=unique_id, updated_by=updated_by)
            logging.info('response: ' + json.dumps(response))
            if code == 'SF10400':
                return custom_failure_response_solar(status_code=200, code=code, message=response['message'],statusCode="CF-300")
            elif code == 'CF10200':
                message = response['message']
                if message== "error":
                    message="Data Not Fetched"
                del response["message"]
                data=response
                return success_response_solar(status_code=200, code=code, message=message, data=data)
            elif code == 'CF12409':
                return custom_failure_response_solar(status_code=200, code=code, message='Customer already exist',statusCode="CF300")
        else:
            if not merchant_id:
                message = "Please provide merchant id"
            elif not mobile:
                message = "Please enter valid mobile number"
            elif not dob:
                message = "Please provide valid DOB"
            elif not pan_number:
                message = "Please enter a Valid PAN Number"
            else:
                message = "error"
            return custom_failure_response_solar(status_code=200, code="CF10400", message=message,statusCode="CF300")

        return custom_failure_response_solar(status_code=200, code=code, message='error',statusCode="CF300")

    except Exception as ex:
        bugsnag.notify(ex)
        logging.exception(traceback.print_exc())
        return custom_failure_response_solar(status_code=400, code='CF10400',message="error",statusCode="CF300")


@emi_api.route("/otp", methods=["POST"], endpoint='otp')
@inject
def generate_otp(emi_service: EMIService = Provide[EMIContainer.emi_service]):
    try:
        logging.info(request.json)
        body = request.get_json(force=True)
        mobile = body['mobile_number']
        result, code, = emi_service.initiate_otp(mobile)
        if result:
            return success_response_solar(status_code=200, code=code, message="sent")
        if code=="CF15400":
            return custom_failure_response_solar(status_code=200, code=code, message="Please enter valid mobile number",statusCode="CF300")
        return custom_failure_response_solar(status_code=200, code=code, message="error",
                                             statusCode="CF300")
    except Exception as ex:
        bugsnag.notify(ex)
        logging.exception(traceback.print_exc())
        return custom_failure_response_solar(status_code=400, code='CF10400',message="error",statusCode="CF300")


@emi_api.route("/verify-otp", methods=["POST"], endpoint='verify-otp')
@inject
def verify_otp(emi_service: EMIService = Provide[EMIContainer.emi_service]):
    try:
        logging.info(request.json)
        body = request.get_json(force=True)
        mobile = int(body['mobile_number'])
        otp = body['otp']
        result, code, data = emi_service.confirm_otp(mobile, otp)
        if result:
            return success_response_solar(status_code=200, code=code, message='verified')
        return custom_failure_response_solar(status_code=200, code=code,message='Please enter valid OTP',statusCode="CF300")
    except Exception as ex:
        bugsnag.notify(ex)
        logging.exception(traceback.print_exc())
        return custom_failure_response_solar(status_code=400, code='CF10400',message="error",statusCode="CF300")


@emi_api.route("/employement-details", methods=["POST"], endpoint='employement-details')
@inject
def employement_details(emi_service: EMIService = Provide[EMIContainer.emi_service]):
    try:
        logging.info(request.json)
        body = request.get_json(force=True)
        application_id = body.get("application_id")
        mobile = body.get("mobile_number")
        net_monthyl_income = body.get("net_monthyl_income")
        employement_type=body.get("employement_type")
        company_name=body.get("company_name")
        gst_number=body.get("gst_number")
        result, code,msg= emi_service.employement_details(application_id,mobile,employement_type,net_monthyl_income,company_name,gst_number)
        if result:
            return success_response_solar(status_code=200, code=code, message=msg)
        return custom_failure_response_solar(status_code=200, code=code, message=msg, statusCode="CF300")
    except Exception as ex:
        bugsnag.notify(ex)
        logging.exception(traceback.print_exc())
        return custom_failure_response_solar(status_code=400, code='CF10400',message="error",statusCode="CF300")


@emi_api.route("/create-loan", methods=["POST"], endpoint='create-loan')
@inject
def create_loan(emi_service: EMIService = Provide[EMIContainer.emi_service]):
    try:
        logging.info(request.json)
        body = request.get_json(force=True)
        is_valid, code, message = validate_create_loan_request(body)
        if is_valid:
            app_id = body['application_id']
            unique_id = body['unique_id']
            first_name = body['first_name']
            middle_name = body['middle_name'] if body['middle_name'] in body else ''
            last_name = body['last_name']
            mobile_number = body['mobile_number']
            email = body['email'] if 'email' in body else None
            address_line_1 = body['address_line_1']
            address_line_2 = body['address_line_2'] if body['address_line_2'] else ''
            address_line_3 = body['address_line_3'] if body['address_line_3'] in body else ''
            city = body['city']
            state = body['state']
            pincode = body['pincode']
            center_id = body['center_id'] if body['center_id'] else None

            status, code, data = emi_service.create_loan(unique_id, first_name, middle_name, last_name, email, mobile_number, address_line_1, address_line_2,
                                                         address_line_3, pincode, app_id, city, state, center_id)
            if status:
                logging.info('response: ' + json.dumps(data))
                return success_response_solar(status_code=200, code=code, data=data)
            if '409' in code:
                return custom_failure_response_solar(status_code=200, code=code, statusCode="CF300")
        return custom_failure_response_solar(status_code=200, code=code, message=message,statusCode="CF300")
    except Exception as ex:
        bugsnag.notify(ex)
        logging.exception(traceback.print_exc())
        return custom_failure_response_solar(status_code=400, code='CF10400',message="error",statusCode="CF300")


@emi_api.route("/electricity-bill", methods=["POST"], endpoint='electricity-bill')
@inject
def process_electricity_bill(emi_service: EMIService = Provide[EMIContainer.emi_service]):
    try:
        logging.info(request.json)
        body = request.get_json(force=True)
        is_valid, code, message = validate_electricity_bill_request(body)
        if is_valid:
            app_id = body['application_id']
            provider = body['provider']
            customer_id = body['customer_id']
            district = body['district'] if 'district' in body else ''
            monthly_bill_amount = body['monthly_bill_amount']
            mobile_number = body.get('mobile_number')
            other_provider = body.get("oth_provider")
            status, code = emi_service.get_electricity_bill_details(app_id, provider, customer_id, district,
                                                                    monthly_bill_amount, mobile_number, other_provider)
            if status:
                return success_response_solar(status_code=200, code=code, message='fetched')
        return custom_failure_response_solar(status_code=200, code=code, message='Failed to validate the details',statusCode="CF300")
    except Exception as ex:
        bugsnag.notify(ex)
        logging.exception(traceback.print_exc())
        return custom_failure_response_solar(status_code=400, code='CF10400',message="error",statusCode="CF300")


@emi_api.route("/product-details", methods=["POST"], endpoint='product-details')
@inject
def product_details(emi_service: EMIService = Provide[EMIContainer.emi_service]):
    try:
        logging.info(request.json)
        body = request.get_json(force=True)
        is_valid, code, message = validate_product_details_request(body)
        if is_valid:
            app_id = body['application_id']
            project = body.get('project')
            loan_amount = body['loan_amount']
            kw_installation = body['kw_installation']
            inverter_to_be_installed = body['inverter_to_be_installed']
            other_inverter = body['other_inverter']
            panel_to_be_installed = body['panel_to_be_installed']
            other_panel = body['other_panel']
            if inverter_to_be_installed == 'Other':
                inverter_to_be_installed = other_inverter
            if panel_to_be_installed == 'Other':
                panel_to_be_installed = other_panel
            subsidy = 0
            status, code = emi_service.process_product_details(app_id, project, loan_amount, kw_installation, inverter_to_be_installed,
                                                               panel_to_be_installed, subsidy)
            if status:
                data = []
                if 'data' in status:
                    data = status['data']
                message = status['message']
                # logging.info('response: ' + json.dumps(status['data']))
                if len(data) > 0:
                    return success_response_solar(status_code=200, code=code, data=data, message=message)
                if message == "Not Approved" or message == 'Bureau not generated':
                    message = "Application submitted,Our team will reach out to you within 24 hrs"
                    return custom_failure_response_solar(status_code=200, code=code, message=message,statusCode="CF301")
                elif message == "Rejected":
                    return custom_failure_response_solar(status_code=200, code=code, message=message,statusCode="CF302")
        return custom_failure_response_solar(status_code=200, code=code, message=message,statusCode="CF300")
    except Exception as ex:
        bugsnag.notify(ex)
        logging.exception(traceback.print_exc())
        return custom_failure_response_solar(status_code=400, code='CF10400',message="error",statusCode="CF300")


@emi_api.route("/get-config", methods=["POST"], endpoint='get-config')
@inject
def get_config(emi_service: EMIService = Provide[EMIContainer.emi_service]):
    try:
        data = emi_service.get_schema_file()
        return data, 200
    except Exception as ex:
        bugsnag.notify(ex)
        logging.exception(traceback.print_exc())
        return failure_response(status_code=400, code='CF10400')


@emi_api.route("/get-serviceprovider-list", methods=["POST"], endpoint='get-serviceprovider-list')
@inject
def get_serviceprovider_list():
    try:
        district = request.args.get('district')
        code = "CF10200"
        if district:
            message = "List of districts of uttar pradesh"
            district_list = config.KARZA_UP_DISTRICT
            district_obj_list = []
            for elem in district_list:
                district_obj_list.append({"id": elem, "name": elem})
            data = {"service": district_obj_list}
        else:
            dict=config.KARZA_ELECTRICTY_PROVIDER
            service=[]
            for i, j in dict.items():
                service.append({"id": j, "name": i})
            data = {"service": service}
            message = "List of service provider"
        return success_response_solar(status_code=200, data=data, message=message, code=code)
    except Exception as ex:
        bugsnag.notify(ex)
        logging.exception(traceback.print_exc())
        return custom_failure_response_solar(status_code=400, code='CF10400',message="error",statusCode="CF300")


@emi_api.route("/get-inverters-list", methods=["POST"], endpoint='get-inverters-list')
@inject
def get_inverters_list(emi_service: EMIService = Provide[EMIContainer.emi_service]):
    try:
        data = emi_service.get_inverters_list()
        service = []
        id = 0
        for i in data:
            id = id + 1
            service.append({"id": id, "name": i})
        code = "CF10200"
        data = {"service": service}
        message = "List of inverters"
       
        return success_response_solar(status_code=200, data=data, message=message, code=code)
    except Exception as ex:
        bugsnag.notify(ex)
        logging.exception(traceback.print_exc())
        return custom_failure_response_solar(status_code=400, code='CF10400', message="error",statusCode="CF300")
    

@emi_api.route("/save-offer", methods=["POST"], endpoint='save-offer')
@inject
def save_offer(emi_service: EMIService = Provide[EMIContainer.emi_service]):
    try:
        logging.info(request.json)
        body = request.get_json(force=True)
        offer_id = body['offer_id']
        application_id = body['application_id']
        status, code = emi_service.save_offer(application_id, offer_id)
        if status:
            return success_response_solar(status_code=200, message='Loan offer updated', code=code)
        return custom_failure_response_solar(status_code=200, code=code, message='Something went wrong', statusCode='CF400')
    except Exception as ex:
        bugsnag.notify(ex)
        logging.exception(traceback.print_exc())
        return custom_failure_response_solar(status_code=400, code='SF10400',essage='error', statusCode='CF300')


@emi_api.route("/get-merchant-name", methods=["POST"], endpoint='get-merchant-name')
@inject
def get_merchant(emi_service: EMIService = Provide[EMIContainer.emi_service]):
    try:
        logging.info(request.json)
        body = request.get_json(force=True)
        merchant_slug = body.get('merchant_slug')
        status, code, data = emi_service.display_merchant_name(merchant_slug)
        dict_data = {}
        if status:
            for item in data:
                dict_data['id'] = item.get('id')
                dict_data['name'] = item.get('merchant_name')
            return success_response_solar(status_code=200, message='Merchant Name', code=code, data=dict_data)
        return custom_failure_response_solar(status_code=200, code=code, message='Merchant not found',
                                             statusCode='CF400')
    except Exception as ex:
        bugsnag.notify(ex)
        logging.exception(traceback.print_exc())
        return custom_failure_response_solar(status_code=400, code='SF10400', essage='error', statusCode='CF300')


@emi_api.route("/get-merchant-center-name", methods=["POST"], endpoint='get-merchant-center-name')
@inject
def get_merchant(emi_service: EMIService = Provide[EMIContainer.emi_service]):
    try:
        logging.info(request.json)
        body = request.get_json(force=True)
        merchant_center_slug = body.get('merchant_center_slug')
        status, code, data = emi_service.display_merchant_center_name(merchant_center_slug)
        dict_data = {}
        if status:
            for item in data:
                dict_data['id'] = item.get('id')
                dict_data['name'] = item.get('name')
            return success_response_solar(status_code=200, message='Merchant Center Name', code=code, data=dict_data)
        return custom_failure_response_solar(status_code=200, code=code, message='Merchant center not found',
                                             statusCode='CF400')
    except Exception as ex:
        bugsnag.notify(ex)
        logging.exception(traceback.print_exc())
        return custom_failure_response_solar(status_code=400, code='SF10400', essage='error', statusCode='CF300')


@emi_api.route("/check-bureau", methods=["POST"], endpoint='check-bureau')
@inject
def check_bureau(emi_service: EMIService = Provide[EMIContainer.emi_service]):
    try:
        logging.info(request.json)
        body = request.get_json(force=True)
        app_id = body['application_id']
        first_name = body['first_name']
        last_name = body['last_name']
        mobile_number = body['mobile_number']
        address_line_1 = body['address_line_1']
        address_line_2 = body['address_line_2'] if body['address_line_2'] else ''
        address_line_3 = body['address_line_3'] if body['address_line_3'] in body else ''
        city = body['city']
        state = body['state']
        pincode = body['pincode']
        dob = body['dob']
        pan_card = body['pan_card']
        status, code = emi_service.get_bureau(app_id, first_name, last_name, pan_card, mobile_number, address_line_1,
                                              address_line_2, address_line_3, pincode, city, state, dob)
        if status:
            return success_response_solar(status_code=200, message='Bureau Fetched', code=code)
        return custom_failure_response_solar(status_code=200, code=code, message='Bureau not pulled',
                                             statusCode='CF400')
    except Exception as ex:
        bugsnag.notify(ex)
        logging.exception(traceback.print_exc())
        return custom_failure_response_solar(status_code=400, code='SF10400', essage='error', statusCode='CF300')


@emi_api.route("/get-user-details", methods=["POST"], endpoint='get-user-details')
@inject
def get_user_details(emi_service: EMIService = Provide[EMIContainer.emi_service]):
    try:
        logging.info(request.json)
        body = request.get_json(force=True)
        mobile_number = body.get('mobile_number')
        status, response, code = emi_service.get_user_journey_details(mobile_number)
        if status:
            return success_response_solar(status_code=200, code=code, data=response, message='User details')
        return custom_failure_response_solar(status_code=200, code=code, statusCode='CF300',
                                             message='No existing application found. Please create the application')
    except Exception as ex:
        bugsnag.notify(ex)
        logging.exception(traceback.print_exc())
        return custom_failure_response_solar(status_code=400, code='SF10400', essage='error', statusCode='CF300')
