import { colors, fonts } from './components/theme';

interface LayoutProps {
    title: string;
    previewText?: string;
    content: string;
}

export function Layout({ title, previewText, content }: LayoutProps) {
    // App branding
    const APP_NAME = 'Proten Job Board';
    const LOGO_URL = process.env.NEXT_PUBLIC_APP_URL ? `${process.env.NEXT_PUBLIC_APP_URL}/images/logo-dark-mode.png` : 'https://protenintl.com/wp-content/uploads/2021/04/Proten-Logo.png';

    return `
<!DOCTYPE html>
<html lang="en" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office">
<head>
  <meta charset="utf-8">
  <meta name="x-apple-disable-message-reformatting">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="format-detection" content="telephone=no, date=no, address=no, email=no">
  <title>${title}</title>
  
  <!--[if mso]>
  <xml><o:OfficeDocumentSettings><o:PixelsPerInch>96</o:PixelsPerInch></o:OfficeDocumentSettings></xml>
  <style>
    td,th,div,p,a,h1,h2,h3,h4,h5,h6 {font-family: "Segoe UI", sans-serif; mso-line-height-rule: exactly;}
  </style>
  <![endif]-->

  <!-- Fonts (Inter) -->
  <link rel="preconnect" href="https://fonts.googleapis.com">
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet" media="screen">

  <style>
    /* Reset & Base Setup */
    body, p, h1, h2, h3, h4, h5, h6 {
      margin: 0;
      padding: 0;
    }
    body {
      font-family: ${fonts.main};
      background-color: #f8fafc;
      color: ${colors.foreground};
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
      width: 100% !important;
    }
    img {
      max-width: 100%;
      border: 0;
      line-height: 100%;
      outline: none;
      text-decoration: none;
    }
    table {
      border-collapse: collapse;
      mso-table-lspace: 0pt;
      mso-table-rspace: 0pt;
    }
    td, th {
      padding: 0;
    }

    /* Core Classes */
    .wrapper {
      width: 100%;
      table-layout: fixed;
      background-color: #f8fafc;
      padding-bottom: 40px;
    }
    .main {
      background-color: #ffffff;
      margin: 0 auto;
      width: 100%;
      max-width: 600px;
      border-radius: 8px;
      box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06);
      overflow: hidden;
    }
    
    /* Dark Mode Media Queries */
    @media (prefers-color-scheme: dark) {
      body, .wrapper {
        background-color: #0f172a !important; /* Very dark blue/slate bg */
      }
      .main {
        background-color: #1e293b !important; /* Slate 800 */
        box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.5) !important;
      }
      h1, h2, h3, h4, h5, h6, p, td, span, a {
        color: #f1f5f9 !important; /* Light text */
      }
      .text-muted {
        color: #94a3b8 !important; /* Slate 400 */
      }
      .header-area {
        border-bottom-color: #334155 !important;
      }
      .footer-area {
        border-top-color: #334155 !important;
      }
      .box {
        background-color: #334155 !important;
        border-color: #475569 !important;
      }
    }

    /* Mobile Media Queries */
    @media screen and (max-width: 600px) {
      .main {
        width: 100% !important;
        border-radius: 0 !important;
      }
      .container-padding {
        padding-left: 16px !important;
        padding-right: 16px !important;
      }
      .mobile-stack {
        display: block !important;
        width: 100% !important;
      }
      .mobile-pt-4 {
        padding-top: 16px !important;
      }
    }
  </style>
</head>
<body>
  ${previewText ? `
  <!-- Preview Text (Hidden in body, visible in inbox list) -->
  <div style="display: none; max-height: 0px; overflow: hidden;">
    ${previewText}
    &nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;&zwnj;&nbsp;
  </div>
  ` : ''}

  <div class="wrapper" style="background-color: #f8fafc;">
    <!-- Spacer above -->
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
      <tr><td height="40" style="font-size: 40px; line-height: 40px;">&nbsp;</td></tr>
    </table>

    <table border="0" cellpadding="0" cellspacing="0" class="main" align="center">
      
      <!-- HEADER -->
      <tr>
        <td class="header-area container-padding" style="padding: 24px 32px; border-bottom: 1px solid ${colors.border}; text-align: left;">
          <a href="${process.env.NEXT_PUBLIC_APP_URL || 'https://protenintl.com'}" target="_blank">
            <!-- Using a fallback text logo if image doesn't load, styled with Inter -->
            <img src="${LOGO_URL}" alt="${APP_NAME}" height="32" style="height: 32px; display: block; filter: grayscale(100%);" />
          </a>
        </td>
      </tr>

      <!-- BODY CONTENT -->
      <tr>
        <td class="container-padding" style="padding: 32px;">
          <div style="font-family: ${fonts.main}; font-size: 16px; line-height: 1.5; color: ${colors.foreground};">
            ${content}
          </div>
        </td>
      </tr>

      <!-- FOOTER -->
      <tr>
        <td class="footer-area container-padding" style="padding: 24px 32px; background-color: #f1f5f9; border-top: 1px solid ${colors.border};">
          <table border="0" cellpadding="0" cellspacing="0" width="100%">
            <tr>
              <td style="font-family: ${fonts.main}; font-size: 13px; line-height: 1.5; color: ${colors.textMuted}; text-align: center;">
                <p style="margin-bottom: 8px;">Questions? Reply to this email or contact <a href="mailto:support@protenintl.com" style="color: ${colors.primary}; text-decoration: underline;">support@protenintl.com</a></p>
                <p style="margin-bottom: 16px;">&copy; ${new Date().getFullYear()} ${APP_NAME}. All rights reserved.</p>
                <p style="font-size: 11px;">
                  <a href="${process.env.NEXT_PUBLIC_APP_URL}/settings/notifications" style="color: ${colors.textMuted}; text-decoration: underline;">Email Preferences</a> | 
                  <a href="${process.env.NEXT_PUBLIC_APP_URL}/unsubscribe" style="color: ${colors.textMuted}; text-decoration: underline;">Unsubscribe</a>
                </p>
              </td>
            </tr>
          </table>
        </td>
      </tr>

    </table>

    <!-- Spacer below -->
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
      <tr><td height="40" style="font-size: 40px; line-height: 40px;">&nbsp;</td></tr>
    </table>
  </div>
</body>
</html>
  `;
}
