export const LANDING_LOCALE_KEY = 'mailhub-landing-locale';
export const supportedLocales = ['zh-CN', 'en-US'];

const messages = {
  'zh-CN': {
    'nav.features': '功能',
    'nav.integrate': '接入',
    'nav.api': 'API',
    'nav.smtp': 'SMTP',
    'nav.webhooks': 'Webhooks',
    'cta.login': '登录',
    'cta.signup': '开始使用',
    'cta.viewApi': '查看 API 示例',
    'cta.github': 'GitHub',
    'common.copy': '复制',
    'common.copied': '已复制',
    'hero.eyebrow': '事务邮件平台',
    'hero.title': '用 API 与 SMTP 发送可靠的事务邮件',
    'hero.lede': '自托管 MailHub 提供域名 DNS 引导、DKIM 签名、投递日志与签名 Webhook —— 无需被黑盒 SaaS 锁定。',
    'hero.curl': `curl -X POST https://mail.example.com/api/send \\
  -H 'Authorization: Bearer &lt;USER_API_TOKEN&gt;' \\
  -H 'Content-Type: application/json' \\
  -d '{
    "from": "noreply@example.com",
    "to": "user@example.com",
    "subject": "Hello from MailHub",
    "text": "Signed with DKIM and queued by MailHub."
  }'`,
    'trust.selfHosted': '自托管',
    'trust.webhooks': '投递 Webhooks',
    'features.title': '为生产发信而设计',
    'features.sub': '完成域名认证、发送与可观测所需的关键能力。',
    'features.dns.title': '域名与 DNS',
    'features.dns.body': 'SPF / DKIM / DMARC 引导，支持 DNS 服务商一键写入。',
    'features.smtp.title': 'SMTP Submission',
    'features.smtp.body': '面向应用的 SMTP 接入，凭据由你掌控。',
    'features.api.title': 'HTTP 发送 API',
    'features.api.body': 'Bearer Token + 简洁 JSON 发送接口，适合服务端集成。',
    'features.logs.title': '投递日志',
    'features.logs.body': '跟踪入队与 MTA 结果，支持检索发送历史。',
    'features.hooks.title': 'Webhooks',
    'features.hooks.body': '对已送达 / 退信 / 失败终态事件进行签名回调。',
    'features.multi.title': '多用户隔离',
    'features.multi.body': '域名、Token 与日志按账号隔离。',
    'integrate.title': '三种接入方式',
    'integrate.sub': '选择适合你技术栈的路径 —— 注册登录后完成配置。',
    'integrate.api.title': 'API',
    'integrate.api.body': '创建 API Token，使用 Bearer 认证 POST JSON 到 /api/send。',
    'integrate.api.link': '查看 API 示例 →',
    'integrate.smtp.title': 'SMTP',
    'integrate.smtp.body': '将应用指向 MailHub Submission 端口，使用独立登录凭据。',
    'integrate.smtp.link': '查看 SMTP 说明 →',
    'integrate.hooks.title': 'Webhooks',
    'integrate.hooks.body': '为终态投递事件订阅端点，校验 HMAC 签名。',
    'integrate.hooks.link': '查看 Webhook 事件 →',
    'api.title': '发送 API',
    'api.sub': '最小 JSON 契约。下列主机名为占位符，请替换为你的 MailHub 地址。',
    'api.curl': `curl -X POST https://mail.example.com/api/send \\
  -H 'Authorization: Bearer &lt;USER_API_TOKEN&gt;' \\
  -H 'Content-Type: application/json' \\
  -d '{
    "from": "noreply@example.com",
    "to": "user@example.com",
    "subject": "Hello from MailHub",
    "text": "Signed with DKIM and queued by MailHub."
  }'`,
    'api.field.from': '已验证域名上的发件地址',
    'api.field.to': '收件人字符串或数组',
    'api.field.subject': '邮件主题',
    'api.field.body': '纯文本或 HTML 正文',
    'smtp.title': 'SMTP Submission',
    'smtp.sub': '默认监听端口（部署时可覆盖）：',
    'smtp.note': '注册后在控制台创建 SMTP 登录凭据。主机示例：mail.example.com',
    'webhooks.title': '投递 Webhooks',
    'webhooks.sub': '仅终态事件 —— 向你的系统发送签名 HTTPS 回调。',
    'webhooks.col.status': '状态',
    'webhooks.col.type': 'type',
    'webhooks.sig': '签名请求头：',
    'ctaBand.title': '准备好发送了吗？',
    'ctaBand.sub': '创建账号、添加域名，几分钟内发出第一封邮件。',
    'footer.tag': '自托管邮件投递控制平面'
  },
  'en-US': {
    'nav.features': 'Features',
    'nav.integrate': 'Integrate',
    'nav.api': 'API',
    'nav.smtp': 'SMTP',
    'nav.webhooks': 'Webhooks',
    'cta.login': 'Log in',
    'cta.signup': 'Get started',
    'cta.viewApi': 'View API sample',
    'cta.github': 'GitHub',
    'common.copy': 'Copy',
    'common.copied': 'Copied',
    'hero.eyebrow': 'Transactional email platform',
    'hero.title': 'Send reliable email via API & SMTP',
    'hero.lede': 'Self-hosted MailHub gives you domain DNS guidance, DKIM signing, delivery logs, and signed webhooks — without locking you into a black-box SaaS.',
    'hero.curl': `curl -X POST https://mail.example.com/api/send \\
  -H 'Authorization: Bearer &lt;USER_API_TOKEN&gt;' \\
  -H 'Content-Type: application/json' \\
  -d '{
    "from": "noreply@example.com",
    "to": "user@example.com",
    "subject": "Hello from MailHub",
    "text": "Signed with DKIM and queued by MailHub."
  }'`,
    'trust.selfHosted': 'Self-hosted',
    'trust.webhooks': 'Delivery webhooks',
    'features.title': 'Built for production senders',
    'features.sub': 'Everything you need to authenticate domains and observe delivery.',
    'features.dns.title': 'Domains & DNS',
    'features.dns.body': 'Guided SPF, DKIM, DMARC, and one-click DNS provider writes.',
    'features.smtp.title': 'SMTP Submission',
    'features.smtp.body': 'App-friendly SMTP listeners with credentials you control.',
    'features.api.title': 'HTTP Send API',
    'features.api.body': 'Bearer tokens and a simple JSON send endpoint for apps and services.',
    'features.logs.title': 'Delivery logs',
    'features.logs.body': 'Track queue and MTA outcomes with searchable send history.',
    'features.hooks.title': 'Webhooks',
    'features.hooks.body': 'Signed callbacks for sent, bounced, and failed terminal events.',
    'features.multi.title': 'Multi-user isolation',
    'features.multi.body': 'Domains, tokens, and logs stay scoped to each account.',
    'integrate.title': 'Three ways to integrate',
    'integrate.sub': 'Pick the path that matches your stack — configure after sign-in.',
    'integrate.api.title': 'API',
    'integrate.api.body': 'Create an API token, then POST JSON to /api/send with Bearer auth.',
    'integrate.api.link': 'See API sample →',
    'integrate.smtp.title': 'SMTP',
    'integrate.smtp.body': 'Point your app at MailHub submission ports with per-user credentials.',
    'integrate.smtp.link': 'See SMTP details →',
    'integrate.hooks.title': 'Webhooks',
    'integrate.hooks.body': 'Subscribe endpoints to terminal delivery events with HMAC signatures.',
    'integrate.hooks.link': 'See webhook events →',
    'api.title': 'Send API',
    'api.sub': 'Minimal JSON contract. Hostnames below are placeholders — replace with your MailHub host.',
    'api.curl': `curl -X POST https://mail.example.com/api/send \\
  -H 'Authorization: Bearer &lt;USER_API_TOKEN&gt;' \\
  -H 'Content-Type: application/json' \\
  -d '{
    "from": "noreply@example.com",
    "to": "user@example.com",
    "subject": "Hello from MailHub",
    "text": "Signed with DKIM and queued by MailHub."
  }'`,
    'api.field.from': 'sender address on a verified domain',
    'api.field.to': 'recipient string or array',
    'api.field.subject': 'message subject',
    'api.field.body': 'plain text or HTML body',
    'smtp.title': 'SMTP submission',
    'smtp.sub': 'Default listener ports (override in your deployment):',
    'smtp.note': 'Create SMTP login credentials in the console after registration. Host example: mail.example.com',
    'webhooks.title': 'Delivery webhooks',
    'webhooks.sub': 'Terminal events only — signed HTTPS callbacks to your systems.',
    'webhooks.col.status': 'Status',
    'webhooks.col.type': 'type',
    'webhooks.sig': 'Signature header:',
    'ctaBand.title': 'Ready to send?',
    'ctaBand.sub': 'Create an account, add a domain, and ship your first message in minutes.',
    'footer.tag': 'Self-hosted email delivery control plane'
  }
};

export function detectLocale() {
  try {
    const stored = localStorage.getItem(LANDING_LOCALE_KEY);
    if (stored && supportedLocales.includes(stored)) return stored;
  } catch {
    /* ignore */
  }
  const nav = typeof navigator !== 'undefined' ? navigator.language || '' : '';
  return nav.toLowerCase().startsWith('zh') ? 'zh-CN' : 'en-US';
}

export function t(locale, key) {
  return messages[locale]?.[key] ?? messages['en-US'][key] ?? key;
}

export function setStoredLocale(locale) {
  try {
    localStorage.setItem(LANDING_LOCALE_KEY, locale);
  } catch {
    /* ignore */
  }
}
