import applicationConfig from '@/constants/application-config.js';
import axios from 'axios';
import { useMainStore } from '@/store';
import loggingEndpointMethods from '@/constants/logging-endpoint-methods';
import headerKeys from '@/constants/header-keys';
import { getSessionKeyValue } from '@/helpers/cookie-helper';

export class Logger {
    logInformation(message, details) {
        this.writeLogEntry(
            loggingEndpointMethods.LOG_INFORMATION,
            this.formatLogEntry(message, details)
        );
    }

    logWarning(message, details) {
        this.writeLogEntry(
            loggingEndpointMethods.LOG_WARNING,
            this.formatLogEntry(message, details)
        );
    }

    logError(message, details) {
        this.writeLogEntry(loggingEndpointMethods.LOG_ERROR, this.formatLogEntry(message, details));
    }

    logCritical(message, details) {
        this.writeLogEntry(
            loggingEndpointMethods.LOG_CRITICAL,
            this.formatLogEntry(message, details)
        );
    }

    formatLogEntry(message, details) {
        return `Application: ${applicationConfig.APPLICATION_NAME}\n${message}\n${
            details ? JSON.stringify(details, undefined, 2) : ''
        }`;
    }

    writeLogEntry(endpoint, logEntry) {
        const store = useMainStore();
        const sessionKey = getSessionKeyValue();

        return new Promise((resolve, reject) => {
            // If running locally or in the Dev environment show the log entries in the console.
            if (
                applicationConfig.CURRENT_ENVIRONMENT === 'Localhost'
                || applicationConfig.CURRENT_ENVIRONMENT === 'Dev'
                || applicationConfig.CURRENT_ENVIRONMENT === 'SysTest'
            ) {
                switch (endpoint) {
                    case loggingEndpointMethods.LOG_INFORMATION:
                        console.info(logEntry);
                        break;
                    case loggingEndpointMethods.LOG_WARNING:
                        console.warn(logEntry);
                        break;
                    case loggingEndpointMethods.LOG_ERROR:
                        console.error(logEntry);
                        break;
                    case loggingEndpointMethods.LOG_CRITICAL:
                        console.error(logEntry);
                        break;
                    default:
                        console.log(logEntry);
                }
            }

            const url =
                applicationConfig.CONSUMER_CF_DISTRO + applicationConfig.FRONTEND_LOGGER_PATH;
            const headers = {
                [headerKeys.EXPERIMENT]: JSON.stringify(store.experimentSettings),
                [headerKeys.APPLICATION_NAME]: applicationConfig.APPLICATION_NAME,
                [headerKeys.TRANSACTION_ID]: crypto.randomUUID(),
                [headerKeys.ENTERPRISE_ORDER_NUMBER]: store.order.eon,
                [headerKeys.REFERRAL_SEQUENCE_NUMBER]: store.order.referralSequenceNumber,
                [headerKeys.SESSION_SEQUENCE_NUMBER]: sessionKey
            };

            axios({
                method: 'POST',
                url: `${url}/${endpoint}`,
                data: { entry: logEntry },
                crossDomain: true,
                responseType: {},
                headers
            }).then(
                (response) => resolve(response),
                (error) => reject(error)
            );
        });
    }
}

export default Logger;
