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 renderPatientFirstResultsReceivedEmail({
  language,
  recipientName,
  analysisOrderId,
}: {
  language: string;
  recipientName: string;
  analysisOrderId: number;
}) {
  const namespace = 'patient-first-results-received-email';

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

  const previewText = t(`${namespace}:previewText`);

  const subject = t(`${namespace}:subject`);

  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(`common:helloName`, { name: recipientName })}
              </Text>
              <Text>
                {t(`${namespace}:p1`)}{' '}
                <Link
                  href={`${process.env.NEXT_PUBLIC_SITE_URL}/home/analysis-results/${analysisOrderId}`}
                >
                  {`${process.env.NEXT_PUBLIC_SITE_URL}/home/analysis-results/${analysisOrderId}`}
                </Link>
              </Text>
              <Text>{t(`${namespace}:p2`)}</Text>
              <Text>{t(`${namespace}:p3`)}</Text>
              <Text>{t(`${namespace}:p4`)}</Text>
              <CommonFooter t={t} />
            </EmailContent>
          </EmailWrapper>
        </Body>
      </Tailwind>
    </Html>,
  );

  return {
    html,
    subject,
  };
}
