import { EmailOptions, Message, EmailRecipient } from '@/shared/email/email.interfaces.js';
import config from '@/shared/config/config.js';
import { TemplateName } from '../constants';
import { getUserByEmail } from '@/modules/user/user.service';
import { sendEmailWithActiveTemplate } from '@/modules/communication/email/email.helper';
import { sendSESEmail } from './sesEmailService';

export const sendTransactionalEmail = async ({
  to,
  cc,
  subject,
  htmlContent,
  textContent,
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  templateId,
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  params,
  sender = { name: config.ses.senderName, email: config.ses.senderEmail },
  attachments,
}: EmailOptions & { cc?: string | string[] | EmailRecipient[] }) => {
  return sendSESEmail({
    to,
    cc,
    subject: subject || '',
    htmlContent,
    textContent,
    attachments,
  });
};

/**
 * Send reset password email
 * @param {string} to
 * @param {string} token
 * @returns {Promise<void>}
 */
export const sendResetPasswordEmail = async (
  to: string,
  token: string,
): Promise<void> => {
  // replace this url with the link to the reset password page of your front-end app
  const resetPasswordUrl = `${config.clientUrl}/resetPassword?token=${token}`;

  const user = await getUserByEmail(to);
  const name = `${user.firstName} ${user.lastName}`;

  await sendEmailWithActiveTemplate({
    to,
    companyId: user.company.id._id,
    scenario: TemplateName.ForgotPassword,
    templateParams: {
      fullName: name,
      hostUrl: resetPasswordUrl,
    },
  });
};

/**
 * Send verification email
 * @param {string} to
 * @param {string} token
 * @param {string} name
 * @returns {Promise<void>}
 */
export const sendVerificationMail = async (
  to: string,
  token: string,
  name: string,
): Promise<void> => {
  const subject = 'Email Verification';
  // replace this url with the link to the email verification page of your front-end app
  const verificationEmailUrl = `http://${config.clientUrl}/verify-email?token=${token}`;
  // const text = `Hi ${name},
  // To verify your email, click on this link: ${verificationEmailUrl}
  // If you did not create an account, then ignore this email.`;
  const html = `<div style="margin:30px; padding:30px; border:1px solid black; border-radius: 20px 10px;"><h4><strong>Hi ${name},</strong></h4>
  <p>To verify your email, click on this link: ${verificationEmailUrl}</p>
  <p>If you did not create an account, then ignore this email.</p></div>`;
  // await sendEmail(to, subject, text, html);
  await sendTransactionalEmail({
    to: to,
    subject: subject,
    htmlContent: html,
  }).catch((err) => {
    console.error('Background sendEmail failed:', err);
    // can manage queue here
  });
};

/**
 * Send email verification after registration
 * @param {string} to
 * @param {string} token
 * @param {string} name
 * @returns {Promise<void>}
 */
export const sendSuccessfulRegistration = async (
  to: string,
  token: string,
  name: string,
): Promise<void> => {
  const subject = 'Email Verification';
  // replace this url with the link to the email verification page of your front-end app
  const verificationEmailUrl = `http://${config.clientUrl}/verify-email?token=${token}`;
  // const text = `Hi ${name},
  // Congratulations! Your account has been created.
  // You are almost there. Complete the final step by verifying your email at: ${verificationEmailUrl}
  // Don't hesitate to contact us if you face any problems
  // Regards,
  // Team`;
  const html = `<div style="margin:30px; padding:30px; border:1px solid black; border-radius: 20px 10px;"><h4><strong>Hi ${name},</strong></h4>
  <p>Congratulations! Your account has been created.</p>
  <p>You are almost there. Complete the final step by verifying your email at: ${verificationEmailUrl}</p>
  <p>Don't hesitate to contact us if you face any problems</p>
  <p>Regards,</p>
  <p><strong>Team</strong></p></div>`;
  // await sendEmail(to, subject, text, html);
  await sendTransactionalEmail({
    to: to,
    subject: subject,
    htmlContent: html,
  }).catch((err) => {
    console.error('Background sendEmail failed:', err);
    // can manage queue here
  });
};

/**
 * Send email verification after registration
 * @param {string} to
 * @param {string} name
 * @returns {Promise<void>}
 */
export const sendAccountCreated = async (
  to: string,
  name: string,
): Promise<void> => {
  const subject = 'Account Created Successfully';
  // replace this url with the link to the email verification page of your front-end app
  const loginUrl = `http://${config.clientUrl}/auth/login`;
  const text = `Hi ${name},
  Congratulations! Your account has been created successfully. 
  You can now login at: ${loginUrl}
  Don't hesitate to contact us if you face any problems
  Regards,
  Team`;
  const html = `<div style="margin:30px; padding:30px; border:1px solid black; border-radius: 20px 10px;"><h4><strong>Hi ${name},</strong></h4>
  <p>Congratulations! Your account has been created successfully.</p>
  <p>You can now login at: ${loginUrl}</p>
  <p>Don't hesitate to contact us if you face any problems</p>
  <p>Regards,</p>
  <p><strong>Team</strong></p></div>`;
  //Changed to transactional Email SES
  sendTransactionalEmail({ to, subject, htmlContent: html, textContent: text });
};
