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

import { BodyStyle } from '../components/body-style';
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 renderCompanyOfferEmail({
  language,
  companyData,
}: {
  language?: string;
  companyData: {
    companyName: string;
    contactPerson: string;
    email: string;
    phone?: string;
  };
}) {
  const namespace = 'company-offer-email';

  const { t } = await initializeEmailI18n({
    language,
    namespace,
  });

  const to = process.env.CONTACT_EMAIL || '';

  const previewText = t(`${namespace}:previewText`, {
    companyName: companyData.companyName,
  });

  const subject = t(`${namespace}:subject`, {
    companyName: companyData.companyName,
  });

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

      <Preview>{previewText}</Preview>

      <Tailwind>
        <Body>
          <EmailWrapper>
            <EmailHeader>
              <EmailHeading>{previewText}</EmailHeading>
            </EmailHeader>

            <EmailContent>
              <Text className="text-[16px] leading-[24px] text-[#242424]">
                {t(`${namespace}:companyName`)} {companyData.companyName}
              </Text>

              <Text className="text-[16px] leading-[24px] text-[#242424]">
                {t(`${namespace}:contactPerson`)} {companyData.contactPerson}
              </Text>

              <Text className="text-[16px] leading-[24px] text-[#242424]">
                {t(`${namespace}:email`)} {companyData.email}
              </Text>

              <Text className="text-[16px] leading-[24px] text-[#242424]">
                {t(`${namespace}:phone`)} {companyData.phone || 'N/A'}
              </Text>
            </EmailContent>
          </EmailWrapper>
        </Body>
      </Tailwind>
    </Html>,
  );

  return {
    html,
    subject,
    to,
  };
}
