function assert(condition: unknown, message = "assertion failed"): void {
  if (!condition) throw new Error(message);
}

function assertEqual<T>(actual: T, expected: T, message = "values should be equal"): void {
  if (actual !== expected) throw new Error(`${message}: expected ${String(expected)}, got ${String(actual)}`);
}

function assertDeepEqual(actual: unknown, expected: unknown, message = "values should be deeply equal"): void {
  const actualJson = JSON.stringify(actual);
  const expectedJson = JSON.stringify(expected);
  if (actualJson !== expectedJson) throw new Error(`${message}: expected ${expectedJson}, got ${actualJson}`);
}

import { renderToStaticMarkup } from "react-dom/server";
import CredentialPanel from "../src/components/CredentialPanel";
import PasswordField from "../src/components/PasswordField";
import MessageDisplayPanel from "../src/components/MessageDisplayPanel";
import MailServerSettingsPanel from "../src/components/mail/MailServerSettingsPanel";
import EmailAddressInput from "../src/components/email/EmailAddressInput";
import { PlatformLanguageProvider } from "../src/i18n/LanguageContext";

function noop() {}

const savedPassword = renderToStaticMarkup(
  <PasswordField value="" saved savedPlaceholder="Saved password configured" onValueChange={noop} />
);
assert(savedPassword.includes('placeholder="Saved password configured"'), "saved password placeholder is rendered");
assert(!savedPassword.includes("password-field-toggle"), "saved empty password does not show reveal button");

const typedPassword = renderToStaticMarkup(
  <PasswordField value="secret" saved savedPlaceholder="Saved password configured" onValueChange={noop} />
);
assert(typedPassword.includes('aria-label="i18n:govoplan-core.show_password.044b852f"'), "typed password shows reveal button");
assert(typedPassword.includes("password-field-toggle"), "typed password gets toggle class");

const translatedAddressInput = renderToStaticMarkup(
  <PlatformLanguageProvider>
    <EmailAddressInput
      value={[]}
      onChange={noop}
      addLabel="i18n:govoplan-core.add_address.a71075c4"
      emptyText="i18n:govoplan-core.no_address_added_yet.809c4247"
      suggestions={[{ name: "Ada Lovelace", email: "ada@example.local" }]}
    />
  </PlatformLanguageProvider>
);
assert(translatedAddressInput.includes("No address added yet"), "email input empty text is translated");
assert(!translatedAddressInput.includes("i18n:govoplan-core.no_address_added_yet.809c4247"), "email input does not leak empty-text i18n key");
assert(translatedAddressInput.includes('aria-label="Type a name and email address, then press Enter"'), "email input aria label is translated");

const savedCredentialPanel = renderToStaticMarkup(
  <CredentialPanel
    values={{ username: "sender", password: "" }}
    onChange={noop}
    savedPassword
    savedPasswordPlaceholder="Saved credential"
    heading="Credentials"
  />
);
assert(savedCredentialPanel.includes("Credentials"), "credential panel renders heading");
assert(savedCredentialPanel.includes('value="sender"'), "credential panel renders username");
assert(savedCredentialPanel.includes('placeholder="Saved credential"'), "credential panel renders saved password placeholder");
assert(!savedCredentialPanel.includes("password-field-toggle"), "saved empty credential does not show reveal button");

const settingsPanel = renderToStaticMarkup(
  <MailServerSettingsPanel
    smtp={{ host: "smtp.example.org", port: 587, username: "sender", password: "", security: "starttls", timeout_seconds: 30 }}
    imap={{ host: "imap.example.org", port: 993, username: "sender", password: "", security: "tls", sent_folder: "auto", timeout_seconds: 30 }}
    onSmtpChange={noop}
    onImapChange={noop}
    smtpPasswordSaved
    smtpSavedPasswordPlaceholder="Saved SMTP password"
    imapPasswordSaved
    imapSavedPasswordPlaceholder="Saved IMAP password"
    onTestSmtp={noop}
    onTestImap={noop}
    onLookupFolders={noop}
    folderLookupResult={{
      ok: true,
      protocol: "imap",
      message: "Folders loaded",
      detected_sent_folder: "Sent",
      folders: [{ name: "INBOX", flags: [] }, { name: "Sent", flags: ["\\Sent"] }]
    }}
    onUseDetectedFolder={noop}
  />
);
assert(settingsPanel.includes("i18n:govoplan-core.test_smtp.e5697981"), "SMTP test action is rendered");
assert(settingsPanel.includes("i18n:govoplan-core.server.cb0cb170"), "server heading is rendered");
assert(settingsPanel.includes("i18n:govoplan-core.credentials.dd097a22"), "credentials heading is rendered");
assert(!settingsPanel.includes("Enable IMAP"), "legacy IMAP enable toggle is not rendered");
assert(settingsPanel.includes('placeholder="Saved SMTP password"'), "SMTP saved credential placeholder is rendered");

const imapSettingsPanel = renderToStaticMarkup(
  <MailServerSettingsPanel
    initialSection="imap"
    smtp={{ host: "smtp.example.org", port: 587, username: "sender", password: "", security: "starttls", timeout_seconds: 30 }}
    imap={{ host: "imap.example.org", port: 993, username: "sender", password: "", security: "tls", sent_folder: "auto", timeout_seconds: 30 }}
    onSmtpChange={noop}
    onImapChange={noop}
    imapPasswordSaved
    imapSavedPasswordPlaceholder="Saved IMAP password"
    onTestImap={noop}
    onLookupFolders={noop}
    folderLookupResult={{
      ok: true,
      protocol: "imap",
      message: "Folders loaded",
      detected_sent_folder: "Sent",
      folders: [{ name: "INBOX", flags: [] }, { name: "Sent", flags: ["\\Sent"] }]
    }}
    onUseDetectedFolder={noop}
  />
);
assert(imapSettingsPanel.includes("i18n:govoplan-core.test_imap.ef1bd79c"), "IMAP test action is rendered");
assert(imapSettingsPanel.includes("i18n:govoplan-core.server.cb0cb170"), "IMAP server heading is rendered");
assert(imapSettingsPanel.includes("i18n:govoplan-core.credentials.dd097a22"), "IMAP credentials heading is rendered");
assert(!imapSettingsPanel.includes("Enable IMAP"), "legacy IMAP enable toggle is not rendered in IMAP tab");
assert(!imapSettingsPanel.includes("i18n:govoplan-core.folders.c603ab65"), "folder lookup action is not rendered in the IMAP tab");
assert(imapSettingsPanel.includes('placeholder="Saved IMAP password"'), "IMAP saved credential placeholder is rendered");
assert(!imapSettingsPanel.includes("Folders loaded"), "folder lookup result is not rendered in the IMAP tab");
assert(!imapSettingsPanel.includes("Default sent folder"), "default sent folder field is not rendered");

const advancedSettingsPanel = renderToStaticMarkup(
  <MailServerSettingsPanel
    initialSection="advanced"
    smtp={{ host: "smtp.example.org", security: "starttls" }}
    imap={{ host: "imap.example.org", security: "tls", sent_folder: "Sent" }}
    onSmtpChange={noop}
    onImapChange={noop}
    onLookupFolders={noop}
    folderLookupResult={{
      ok: true,
      protocol: "imap",
      message: "Folders loaded",
      detected_sent_folder: "Sent",
      folders: [{ name: "INBOX", flags: [] }, { name: "Sent", flags: ["\\Sent"] }]
    }}
    onUseDetectedFolder={noop}
  />
);
assert(advancedSettingsPanel.includes("i18n:govoplan-core.append_target_folder.0aaacc0c"), "append target folder is the advanced IMAP folder field");
assert(advancedSettingsPanel.includes("i18n:govoplan-core.folders.c603ab65"), "folder lookup action is rendered near append target folder");
assert(advancedSettingsPanel.includes("Folders loaded"), "folder lookup result is rendered in Advanced");
assert(advancedSettingsPanel.includes("i18n:govoplan-core.use_detected_folder.5ec4965c"), "detected folder action is rendered near append target folder");
assert(advancedSettingsPanel.includes('value="Sent"'), "append target folder uses IMAP sent folder value without append override");

const disabledAppendFolderPanel = renderToStaticMarkup(
  <MailServerSettingsPanel
    initialSection="advanced"
    smtp={{ host: "smtp.example.org", security: "starttls" }}
    imap={{ host: "imap.example.org", security: "tls", sent_folder: "Sent" }}
    onSmtpChange={noop}
    onImapChange={noop}
    onLookupFolders={noop}
    folderLookupResult={{
      ok: true,
      protocol: "imap",
      message: "Folders loaded",
      detected_sent_folder: "Sent",
      folders: [{ name: "Sent", flags: ["\\Sent"] }]
    }}
    onUseDetectedFolder={noop}
    append={{
      enabled: true,
      folder: "Sent",
      folderDisabled: true,
      onEnabledChange: noop,
      onFolderChange: noop
    }}
  />
);
assert(!disabledAppendFolderPanel.includes("i18n:govoplan-core.folders.c603ab65"), "folder lookup action is hidden when append target is disabled");
assert(!disabledAppendFolderPanel.includes("i18n:govoplan-core.use_detected_folder.5ec4965c"), "detected folder action is hidden when append target is disabled");

const smtpServerOnlyPanel = renderToStaticMarkup(
  <MailServerSettingsPanel
    visibleSections={["smtp"]}
    mode="server"
    smtp={{ host: "smtp.example.org", port: 587, username: "sender", password: "", security: "starttls", timeout_seconds: 30 }}
    imap={{ host: "imap.example.org", port: 993, username: "reader", password: "", security: "tls", sent_folder: "auto", timeout_seconds: 30 }}
    onSmtpChange={noop}
    onImapChange={noop}
  />
);
assert(smtpServerOnlyPanel.includes("i18n:govoplan-core.server.cb0cb170"), "focused server panel renders server fields");
assert(!smtpServerOnlyPanel.includes("i18n:govoplan-core.credentials.dd097a22"), "focused server panel hides credential fields");
assert(!smtpServerOnlyPanel.includes("i18n:govoplan-core.mail_server_settings_sections.40a163b7"), "single focused section hides section switcher");
assert(!smtpServerOnlyPanel.includes("imap.example.org"), "focused SMTP server panel does not render IMAP fields");

const imapCredentialsOnlyPanel = renderToStaticMarkup(
  <MailServerSettingsPanel
    initialSection="imap"
    visibleSections={["imap"]}
    mode="credentials"
    smtp={{ host: "smtp.example.org", port: 587, username: "sender", password: "", security: "starttls", timeout_seconds: 30 }}
    imap={{ host: "imap.example.org", port: 993, username: "reader", password: "", security: "tls", sent_folder: "auto", timeout_seconds: 30 }}
    onSmtpChange={noop}
    onImapChange={noop}
    imapPasswordSaved
    imapSavedPasswordPlaceholder="Saved IMAP password"
  />
);
assert(imapCredentialsOnlyPanel.includes("i18n:govoplan-core.credentials.dd097a22"), "focused credentials panel renders credential fields");
assert(!imapCredentialsOnlyPanel.includes("i18n:govoplan-core.server.cb0cb170"), "focused credentials panel hides server fields");
assert(imapCredentialsOnlyPanel.includes('placeholder="Saved IMAP password"'), "focused credentials panel preserves saved password UX");
assert(!imapCredentialsOnlyPanel.includes("smtp.example.org"), "focused IMAP credentials panel does not render SMTP fields");

const defaultPortSettingsPanel = renderToStaticMarkup(
  <MailServerSettingsPanel
    smtp={{ host: "smtp.example.org", security: "starttls" }}
    imap={{ host: "imap.example.org", security: "tls" }}
    onSmtpChange={noop}
    onImapChange={noop}
  />
);
const defaultImapPortSettingsPanel = renderToStaticMarkup(
  <MailServerSettingsPanel
    initialSection="imap"
    smtp={{ host: "smtp.example.org", security: "starttls" }}
    imap={{ host: "imap.example.org", security: "tls" }}
    onSmtpChange={noop}
    onImapChange={noop}
  />
);
assert(defaultPortSettingsPanel.includes('value="587"'), "SMTP starttls default port is displayed");
assert(defaultImapPortSettingsPanel.includes('value="993"'), "IMAP TLS default port is displayed");

const messageDisplay = renderToStaticMarkup(
  <MessageDisplayPanel
    title="Quarterly update"
    fields={[
      { label: "From", value: "Sender <sender@example.org>" },
      { label: "Cc", value: null },
      { label: "Bcc", value: "Audit <audit@example.org>" }
    ]}
    bodyText="Plain body"
    bodyHtml="<p><strong>HTML body</strong></p>"
    preferredBodyMode="html"
    attachments={[
      { filename: "agenda.pdf", contentType: "application/pdf", sizeBytes: 2048 },
      {
        filename: "secret.pdf",
        contentType: "application/pdf",
        archiveGroup: "archive-1",
        archiveLabel: "recipient-files.zip",
        protected: true,
        protectionNote: 'Password-protected ZIP using local field "zip_password". Encryption: AES.'
      }
    ]}
  />
);
assert(messageDisplay.includes("Quarterly update"), "message subject is rendered");
assert(messageDisplay.includes("Sender &lt;sender@example.org&gt;"), "sender field is rendered");
assert(!messageDisplay.includes("<dt>Cc</dt>"), "empty optional CC field is hidden");
assert(messageDisplay.includes("<dt>Bcc</dt>"), "present BCC field is rendered");
assert(messageDisplay.includes("i18n:govoplan-core.html.9f738ce8"), "HTML body switch is rendered");
assert(messageDisplay.includes("i18n:govoplan-core.text.c3328c39"), "text body switch is rendered");
assert(messageDisplay.includes("agenda.pdf"), "direct attachment is rendered");
assert(messageDisplay.includes("recipient-files.zip"), "ZIP archive attachment group is rendered");
assert(messageDisplay.includes("i18n:govoplan-core.password_protected.09d9e174"), "ZIP protection badge is rendered");
assert(messageDisplay.includes("zip_password"), "ZIP protection note is rendered");
