import {
  Body,
  Head,
  Html,
  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 { EmailButton } from '../components/email-button';
import { EmailHeader } from '../components/header';
import { EmailHeading } from '../components/heading';
import { EmailWrapper } from '../components/wrapper';
import { initializeEmailI18n } from '../lib/i18n';

export async function renderFirstResultsReceivedEmail({
  language,
  analysisOrderId,
}: {
  language: string;
  analysisOrderId: number;
}) {
  const namespace = '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(`${namespace}:hello`)}
              </Text>
              <Text className="text-[16px] leading-[24px] text-[#242424]">
                {t(`${namespace}:resultsReceivedForOrders`)}
              </Text>
              <Text className="text-[16px] leading-[24px] font-semibold text-[#242424]">
                {t(`${namespace}:openOrdersHeading`)}
              </Text>

              <EmailButton
                href={`${process.env.NEXT_PUBLIC_SITE_URL}/doctor/analysis/${analysisOrderId}`}
              >
                {t(`${namespace}:linkText`)}
              </EmailButton>

              <Text>
                {t(`${namespace}:ifLinksDisabled`)}{' '}
                {`${process.env.NEXT_PUBLIC_SITE_URL}/doctor/analysis/${analysisOrderId}`}
              </Text>
              <CommonFooter t={t} />
            </EmailContent>
          </EmailWrapper>
        </Body>
      </Tailwind>
    </Html>,
  );

  return {
    html,
    subject,
  };
}
