// WARNING: This file is imported by the frontend too!

// Discord channel: #monitoring-sendgrid
const DISCORD_MONITORING_SENDGRID_WEBHOOK_URL =
  'https://discord.com/api/webhooks/1185268864817319966/5lb5PtqADbyLZuB-aO6M25J6UqPbEpQuf1hkGnfDtSF926lv2OxW1mLvseRBUy_0YPjZ';

// Discord channel: #monitoring-sendgrid-errors
const DISCORD_SENDGRID_ERRORS_WEBHOOK_URL =
  'https://discord.com/api/webhooks/1185423737135304734/ceDG82wlut19hLzBV_RcTsY7pWEFxk7MduSpvQDcddy2Ozc1NKDb5lKRkv0T5JI_XjnZ';

// Discord channel: #monitoring-fusionauth-errors
const DISCORD_FUSIONAUTH_ERRORS_WEBHOOK_URL =
  'https://discord.com/api/webhooks/1202009312038232114/G46yTacrUUALaYS48EBSTBxw27Im3ueaBQn_c8mbgtmhfTZVyZRiiD9Px55q2tgiGng0';

// Discord channel: #monitoring-payment-errors
const DISCORD_PAYMENT_ERROR_WEBHOOK_URL =
  'https://discord.com/api/webhooks/1261190410848440330/9QCB95MKWj7rYeBXQyGiWjVsJEf1SjlbPTGlkv7PyvmcoOdUsfRxwfg3wqg7pg4pVDCH';

// Discord channel: #monitoring-user-deletion_requests
const DISCORD_DELETION_REQUEST_WEBHOOK_URL =
  'https://discord.com/api/webhooks/1202966354018308156/ooiwIQwNK4UtdQoJZGoX2PK-86vnzgQb5K0DY-gF6yKqQeBKmCKn66qUcsUKt0L0_x06';

// Discord channel: #team-support
const DISCORD_SUPPORT_REQUEST_WEBHOOK_URL =
  'https://discord.com/api/webhooks/1204310367883370588/-pGy2jFA-OjWNwAzmKVKxw_Mkfyn220PmnJUgxoGtt9TGqobNMOG5FjZUYAdna15H1Nm';

// Discord channel: #monitoring-release-notes
const DISCORD_RELEASE_NOTES_WEBHOOK_URL =
  'https://discord.com/api/webhooks/1245617105357770832/v5_4MpoVhenS2YfmwjF3xNGT_idm7Z4I9Hdm3wacI8spw4H0zH8wBkmELDsudODi8ZhS';

// Discord channel: #team-standup
const DISCORD_STANDUPS_WEBHOOK_URL =
  'https://discord.com/api/webhooks/1267546047190925314/WfIlvJhmaJFpCoKwGZn8xC8j711C9OjP34eGx8lvet-DpJaPx7HB23stIpPpzVnhCGLv';

// Discord channel: #monitoring-dev [use this channel for development purposes]
const DISCORD_DEV_WEBHOOK_URL =
  'https://discord.com/api/webhooks/1268625156889120931/QLhxaqlQl7kkjVaySRDttoghNeK1nL06RAf_J3czrY3lPg4K7bWGT-G5cG45xsucavlq';

const sendDiscordMessage = async (webhookUrl: string, message: string, botName: string = 'SendGrid sync bot') => {
  var headers = new Headers();
  headers.append('Content-Type', 'application/json');

  let actualWebhookURL = webhookUrl;

  // If in development, send to the dev channel instead of the actual channel
  if (process.env.ENVIRONMENT === 'development') {
    actualWebhookURL = DISCORD_DEV_WEBHOOK_URL;
    botName = botName + ' [DEV]';
  }

  const payload = JSON.stringify({
    username: botName,
    content: message,
  });

  try {
    await fetch(actualWebhookURL, {
      method: 'POST',
      headers: headers,
      body: payload,
    });
  } catch (error) {
    console.log('error', error);
  }
};

export const sendDiscordMonitoringMessage = async (message: string, botName: string = 'SendGrid sync bot') => {
  await sendDiscordMessage(DISCORD_MONITORING_SENDGRID_WEBHOOK_URL, message, botName);
};

export const sendDiscordErrorMessage = async (message: string, botName: string = 'SendGrid sync bot') => {
  await sendDiscordMessage(DISCORD_SENDGRID_ERRORS_WEBHOOK_URL, message, botName);
};

export const sendDiscordFusionAuthMessage = async (message: string, botName: string = 'FusionAuth error bot') => {
  await sendDiscordMessage(DISCORD_FUSIONAUTH_ERRORS_WEBHOOK_URL, message, botName);
};

export const sendDiscordPaymentErrorMessage = async (message: string, botName: string = 'Payment Error Bot') => {
  await sendDiscordMessage(DISCORD_PAYMENT_ERROR_WEBHOOK_URL, message, botName);
};

export const sendDiscordDeletionRequestMessage = async (message: string, botName: string = 'Deletobot') => {
  await sendDiscordMessage(DISCORD_DELETION_REQUEST_WEBHOOK_URL, message, botName);
};

export const sendDiscordSupportRequestMessage = async (message: string, botName: string = 'Support Request Bot') => {
  await sendDiscordMessage(DISCORD_SUPPORT_REQUEST_WEBHOOK_URL, message, botName);
};

export const sendDiscordReleaseNotesMessage = async (message: string, botName: string = 'Release Notes Bot') => {
  await sendDiscordMessage(DISCORD_RELEASE_NOTES_WEBHOOK_URL, message, botName);
};

export const sendDiscordStandupMessage = async (message: string, botName: string = 'Standup Bot') => {
  await sendDiscordMessage(DISCORD_STANDUPS_WEBHOOK_URL, message, botName);
};
