import {
  Body,
  Head,
  Html,
  Link,
  Preview,
  Tailwind,
  Text,
  render,
} from '@react-email/components';

import { BodyStyle } from '../components/body-style';
import CommonFooter from '../components/common-footer';
import { EmailContent } from '../components/content';
import { EmailHeader } from '../components/header';
import { EmailHeading } from '../components/heading';
import { EmailWrapper } from '../components/wrapper';
import { initializeEmailI18n } from '../lib/i18n';

export async function renderNewJobsAvailableEmail({
  language,
  analysisResponseIds,
}: {
  language?: string;
  analysisResponseIds: number[];
}) {
  const namespace = 'new-jobs-available-email';

  const { t } = await initializeEmailI18n({
    language,
    namespace: [namespace, 'common'],
  });

  const previewText = t(`${namespace}:previewText`, {
    nr: analysisResponseIds.length,
  });

  const subject = t(`${namespace}:subject`, {
    nr: analysisResponseIds.length,
  });

  const html = await render(
    <Html>
      <Head>
        <BodyStyle />
      </Head>

      <Preview>{previewText}</Preview>

      <Tailwind>
        <Body>
          <EmailWrapper>
            <EmailContent>
              <EmailHeader>
                <EmailHeading>{previewText}</EmailHeading>
              </EmailHeader>
              <Text className="text-[16px] leading-[24px] text-[#242424]">
                {t(`${namespace}:hello`)}
              </Text>
              <Text className="text-[16px] leading-[24px] text-[#242424]">
                {t(`${namespace}:resultsReceivedForOrders`, {
                  nr: analysisResponseIds.length,
                })}
              </Text>
              <Text className="text-[16px] leading-[24px] font-semibold text-[#242424]">
                {t(`${namespace}:openOrdersHeading`, {
                  nr: analysisResponseIds.length,
                })}
              </Text>
              <ul className="list-none text-[16px] leading-[24px]">
                {analysisResponseIds.map((analysisResponseId, index) => (
                  <li>
                    <Link
                      key={analysisResponseId}
                      href={`${process.env.NEXT_PUBLIC_SITE_URL}/doctor/analysis/${analysisResponseId}`}
                    >
                      {t(`${namespace}:linkText`, { nr: index + 1 })}
                    </Link>
                  </li>
                ))}
              </ul>
              <Text>
                {t(`${namespace}:ifLinksDisabled`)}{' '}
                {`${process.env.NEXT_PUBLIC_SITE_URL}/doctor/open-jobs`}
              </Text>
              <CommonFooter t={t} />
            </EmailContent>
          </EmailWrapper>
        </Body>
      </Tailwind>
    </Html>,
  );

  return {
    html,
    subject,
  };
}
