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 { EmailHeader } from '../components/header';
import { EmailHeading } from '../components/heading';
import { EmailWrapper } from '../components/wrapper';
import { initializeEmailI18n } from '../lib/i18n';

interface Props {
  analysisPackageName: string;
  language?: string;
  personName: string;
  partnerLocationName: string;
}

export async function renderSynlabAnalysisPackageEmail(props: Props) {
  const namespace = 'synlab-email';

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

  const previewText = t(`${namespace}:previewText`, {
    analysisPackageName: props.analysisPackageName,
  });

  const subject = t(`${namespace}:subject`, {
    analysisPackageName: props.analysisPackageName,
  });

  const heading = t(`${namespace}:heading`, {
    analysisPackageName: props.analysisPackageName,
  });

  const hello = t(`${namespace}:hello`, {
    personName: props.personName,
  });

  const lines = [
    t(`${namespace}:lines1`, {
      analysisPackageName: props.analysisPackageName,
      partnerLocationName: props.partnerLocationName,
    }),
    t(`${namespace}:lines2`),
    t(`${namespace}:lines3`),
    t(`${namespace}:lines4`),
    t(`${namespace}:lines5`),
    t(`${namespace}:lines6`),
  ];

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

      <Preview>{previewText}</Preview>

      <Tailwind>
        <Body>
          <EmailWrapper>
            <EmailContent>
              <EmailHeader>
                <EmailHeading>{heading}</EmailHeading>
              </EmailHeader>
              <Text className="text-[16px] leading-[24px] text-[#242424]">
                {hello}
              </Text>
              {lines.map((line, index) => (
                <Text
                  key={index}
                  className="text-[16px] leading-[24px] text-[#242424]"
                  dangerouslySetInnerHTML={{ __html: line }}
                />
              ))}
              <CommonFooter t={t} />
            </EmailContent>
          </EmailWrapper>
        </Body>
      </Tailwind>
    </Html>,
  );

  return {
    html,
    subject,
  };
}
