package com.banesco.commons.config;

import com.banesco.common.application.exception.BanRuntimeException;
import com.banesco.common.domain.dto.bian.device.DateHelper;
import com.banesco.common.domain.dto.bian.device.JsonHelper;
import com.banesco.common.domain.dto.bian.device.RequestHelper;
import com.banesco.common.domain.model.ApiConfig;
import com.banesco.common.domain.model.BackResponse;
import com.banesco.common.infraestructure.repository.MessageRepository;
import com.banesco.common.infraestructure.service.MessageService;
import com.banesco.commons.exceptions.BanConfigException;
import com.banesco.domain.models.CurrentState;
import jakarta.inject.Singleton;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;

import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;


@Singleton
@Getter
@Slf4j
public class AppConf {

    private static AppConf _INSTANCE;

    public final Map<String, BackResponse> responsesMap = new HashMap<>();
    public final MessageService messageService;
    private final String appSourceId;
    private final String recUpdateCardMessageKey;
    private final Config configEnv;
    private final RequestHelper requestHelper;

    DateHelper dateHelper = DateHelper.getInstance();
    public static void getInstance() {

        synchronized (AppConf.class) {
            if (_INSTANCE == null) {
                log.info("AppConf getInstance-->");
                _INSTANCE = new AppConf();
            }
        }
    }

    public AppConf() {

        log.info("NEW INSTANCE AppConf**********************************");
        try {

            configEnv = ConfigProvider.getConfig();

            requestHelper = new RequestHelper();

            messageService = new MessageService(new MessageRepository());

            appSourceId = configEnv.getValue("api.source-id", String.class);

            recUpdateCardMessageKey = configEnv.getValue("api.recUpdateCardStatus.messages.key", String.class);
            boolean apiReadMessages = configEnv.getValue("api.read-messages.from-props", Boolean.class);
            if (apiReadMessages) {
                messageService.loadMessageFromJson(recUpdateCardMessageKey, configEnv.getValue("api.recUpdateCardStatus.messages.content", String.class));
            }
            _INSTANCE = this;
        } catch (NoSuchElementException e) {
            String errorMessage = "[ERROR CONFIG]: [%s]".formatted(e.getMessage());
            log.error(errorMessage);
            throw new BanConfigException(errorMessage);
        } catch (Exception e) {
            String errorMessage = "[ERROR CONFIG]: [%s]".formatted(e.getMessage());
            log.error(errorMessage);
            throw new BanConfigException(errorMessage);
        }

    }


    public ApiConfig loadApiConfig(String apiName) {

        try {

            log.info("Loading APIConfig with key:{}", apiName);
            String apiConfig = configEnv.getValue(apiName, String.class);
            return JsonHelper.getObjectFromJson(apiConfig, ApiConfig.class);
        } catch (Exception e) {
            log.error("Error Loading API Config: {} -> {}", apiName, e.getMessage());
            throw new BanRuntimeException(StatusCodes.INTERNAL_ERROR);
        }

    }

    public CurrentState getCurrentState(String fintechId, String appId) {

        return new CurrentState(
                this.requestHelper.getRequestId(requestHelper.getInstanceId(appSourceId)),
                fintechId,
                appId
        );
    }


}
