import { renderNewJobsAvailableEmail } from '@kit/email-templates';

import { getDoctorAccounts } from '~/lib/services/account.service';
import { getOpenJobAnalysisResponseIds } from '~/lib/services/doctor-jobs.service';
import { sendEmailFromTemplate } from '~/lib/services/mailer.service';

export default async function sendOpenJobsEmails() {
  const analysisResponseIds = await getOpenJobAnalysisResponseIds();

  const doctorAccounts = await getDoctorAccounts();
  const doctorEmails: string[] = doctorAccounts
    .map(({ email }) => email)
    .filter((email): email is string => !!email);

  await sendEmailFromTemplate(
    renderNewJobsAvailableEmail,
    {
      language: 'et',
      analysisResponseIds,
    },
    doctorEmails,
  );
}
