import axios from "axios";
import log from "../config/log";
import clientConfigDB from "../models/clientConfig.model";
import CONSTANTS from "../config/constants";
import whatsAppTemplateDB from "../models/whatsappTemplate.model";

const getClientWhatsappCredentials = async (
  clientId: number,
  propId: number,
  templateType: number = 0) => {
  let F = "WhatsApp Credentials";
  try {
    let whatsAppKey = process.env.WHATSAPP_API_KEY;
    if(0 === clientId){
      let data = {
        key: whatsAppKey,
      }
      return data;
    }
    let whatsAppCredType = "DEFAULT";
    let whatsAppCred = await clientConfigDB.getClientConfigByPropId({
      clientId,
      propId,
      provider: CONSTANTS.CLIENT_CONFIG_PROVIDER.INTERACT,
      type: CONSTANTS.CLIENT_CONFIG_TYPE.KEY
    });
    if(whatsAppCred) {
      whatsAppCredType = "PROPERTY";
      whatsAppKey = `Basic ${whatsAppCred?.value}`
    } else {
        whatsAppCred = await clientConfigDB.getClientConfig(
        {
          clientId,
          provider: CONSTANTS.CLIENT_CONFIG_PROVIDER.INTERACT,
          type: CONSTANTS.CLIENT_CONFIG_TYPE.KEY
        });
        if(whatsAppCred) {
          whatsAppCredType = "CLIENT";
          whatsAppKey = `Basic ${whatsAppCred?.value}`
        }   
    }
    if( "CLIENT" === whatsAppCredType) {
        log.info(
          `[${F}], Client Id [${clientId}], using client credentials`
        );
    } else if("PROPERTY" === whatsAppCredType) {
      log.info(
        `[${F}], Client Id [${clientId}], Prop Id [${propId}], Using client property credentials`
      );
    } else {
      log.info(
        `[${F}], Client Id [${clientId}], Using default Kipinn credentials.`
      );
    }


    let data = {
      key: whatsAppKey,
    }
    // log.info(
    //     `[${F}], ${JSON.stringify(data)}, Credentials`
    //   );
    return data;
  } catch (error: any) {
    log.info(
      `[GET Whatsapp Credentails], Client Id [${clientId}], Prop Id [${propId}], Error: ${JSON.stringify(error)}`
    );
    let data = {
      key: process.env.WHATSAPP_API_KEY,
    }
    return data;
  }
};

export const getClientWhatsappCredentialsChatMitra = async (
  clientId: number,
  propId: number,
  templateType: number = 0,
  userType : number = CONSTANTS.USER_TYPE.TENANT
) => {
  let F = "WhatsApp Credentials Chat Mitra";
  try {
    let templateName = "";
    let lang = "";
    let whatsAppKey = "";
    let templateData = await whatsAppTemplateDB.getByClientAndType({clientId,
      provider: CONSTANTS.CLIENT_CONFIG_PROVIDER.CHAT_MITRA,
      type: templateType,
      userType
    });
    if(!templateData) {
      log.info(`[${F}], Client Id [${clientId}], Template Type [${templateType}], failed to get templateData`);
      return false;
    } else {
      templateName = templateData['templateId'];
      lang = templateData['languageCode'];
    }

    let whatsAppCredType = "DEFAULT";
    let whatsAppCred = await clientConfigDB.getClientConfigByPropId({
      clientId,
      propId,
      provider: CONSTANTS.CLIENT_CONFIG_PROVIDER.CHAT_MITRA,
      type: CONSTANTS.CLIENT_CONFIG_TYPE.KEY
    });
    if(whatsAppCred) {
      whatsAppCredType = "PROPERTY";
      whatsAppKey = `Bearer ${whatsAppCred?.value}`
    } else {
        whatsAppCred = await clientConfigDB.getClientConfig(
        {
          clientId,
          provider: CONSTANTS.CLIENT_CONFIG_PROVIDER.CHAT_MITRA,
          type: CONSTANTS.CLIENT_CONFIG_TYPE.KEY
        });
        if(whatsAppCred) {
          whatsAppCredType = "CLIENT";
          whatsAppKey = `Bearer ${whatsAppCred?.value}`
        }   
    }
    if( "CLIENT" === whatsAppCredType) {
        log.info(
          `[${F}], Client Id [${clientId}], using client credentials`
        );
    } else if("PROPERTY" === whatsAppCredType) {
      log.info(
        `[${F}], Client Id [${clientId}], Prop Id [${propId}], Using client property credentials`
      );
    } else {
      return false;
    }
    let data = {
      key: whatsAppKey,
      templateName: templateName,
      languageCode: lang,
    }
    return data;
  } catch (error: any) {
    log.info(
      `[${F}], Client Id [${clientId}], Prop Id [${propId}], Error: ${JSON.stringify(error)}`
    );

    return false;
  }
};

export const getClientWhatsappCredentialsMessageCentral = async (
  clientId: number,
  propId: number,
  templateType: number = 0,
  userType : number = CONSTANTS.USER_TYPE.TENANT
) => {
  let F = "WhatsApp Credentials Messsage Central";
  try {
    let templateName = "";
    let projectId = "";
    let lang = "";
    let whatsAppKey = process.env.WHATSAPP_API_KEY_MC;
    let templateData = await whatsAppTemplateDB.getByClientAndType({
      clientId,
      provider: CONSTANTS.CLIENT_CONFIG_PROVIDER.MESSAGE_CENTRAL,
      type: templateType,
      userType
    });
    if(!templateData) {
      // log.info(`[${F}], Client Id [${clientId}], Template Type [${templateType}], User Type [${userType}], failed to get templateData`);
      templateData = await whatsAppTemplateDB.getByClientAndType({
        clientId: 0,
        provider: CONSTANTS.CLIENT_CONFIG_PROVIDER.MESSAGE_CENTRAL,
        type: templateType,
        userType
      });
      if(!templateData) {
        // log.info(`[${F}], Client Id [${clientId}], Template Type [${templateType}], User Type [${userType}], failed to get templateData of Kipinn too`);
        return false;
      } else {
        log.info(`[${F}], Client Id [${clientId}], Template Type [${templateType}], User Type [${userType}], Fetched kipinn template detail`);
        projectId = templateData['projectId'];
        templateName = templateData['templateId'];
        lang = templateData['languageCode'];
      }
    } else {
      log.info(`[${F}], Client Id [${clientId}], Template Type [${templateType}], User Type [${userType}], Fetched client template detail`);
      projectId = templateData['projectId'];
      templateName = templateData['templateId'];
      lang = templateData['languageCode'];
    }

    let whatsAppCredType = "DEFAULT";
    let whatsAppCred = await clientConfigDB.getClientConfigByPropId({
      clientId,
      propId,
      provider: CONSTANTS.CLIENT_CONFIG_PROVIDER.MESSAGE_CENTRAL,
      type: CONSTANTS.CLIENT_CONFIG_TYPE.KEY
    });
    if(whatsAppCred) {
      whatsAppCredType = "PROPERTY";
      whatsAppKey = `${whatsAppCred?.value}`
    } else {
        whatsAppCred = await clientConfigDB.getClientConfig(
        {
          clientId,
          provider: CONSTANTS.CLIENT_CONFIG_PROVIDER.MESSAGE_CENTRAL,
          type: CONSTANTS.CLIENT_CONFIG_TYPE.KEY
        });
        if(whatsAppCred) {
          whatsAppCredType = "CLIENT";
          whatsAppKey = `${whatsAppCred?.value}`
        }   
    }
    if( "CLIENT" === whatsAppCredType) {
        log.info(
          `[${F}], Client Id [${clientId}], using client credentials`
        );
    } else if("PROPERTY" === whatsAppCredType) {
      log.info(
        `[${F}], Client Id [${clientId}], Prop Id [${propId}], Using client property credentials`
      );
    } else {
      log.info(
        `[${F}], Client Id [${clientId}], Prop Id [${propId}], Using Default Kipinn credentials`
      );
    }
    let data = {
      key: whatsAppKey,
      templateName: templateName,
      languageCode: lang,
      projectId: projectId,
    }
    return data;
  } catch (error: any) {
    log.info(
      `[${F}], Client Id [${clientId}], Prop Id [${propId}], Error: ${JSON.stringify(error)}`
    );

    return false;
  }
};

export default getClientWhatsappCredentials;
