import type { EmailTemplate } from './types.js';

// ── Built-In Email Templates ─────────────────────────────────
// Day-1 templates for transactional email. Variables are interpolated
// at render time using {{variableName}} syntax.

export const BUILT_IN_TEMPLATES: EmailTemplate[] = [
  {
    id: 'welcome',
    name: 'Welcome Email',
    subject: 'Welcome to {{productName}}!',
    bodyHtml: `
      <h1>Welcome, {{displayName}}!</h1>
      <p>Thanks for signing up for {{productName}}. We're excited to have you on board.</p>
      <p>Get started by exploring the dashboard:</p>
      <p><a href="{{dashboardUrl}}">Go to Dashboard →</a></p>
      <p>If you have any questions, just reply to this email.</p>
      <p>— The {{productName}} Team</p>
    `.trim(),
    bodyText: `Welcome, {{displayName}}!\n\nThanks for signing up for {{productName}}.\n\nGet started: {{dashboardUrl}}\n\n— The {{productName}} Team`,
    variables: ['displayName', 'productName', 'dashboardUrl'],
  },
  {
    id: 'password-reset',
    name: 'Password Reset',
    subject: 'Reset your {{productName}} password',
    bodyHtml: `
      <h1>Password Reset</h1>
      <p>Hi {{displayName}},</p>
      <p>We received a request to reset your password. Click the link below to set a new one:</p>
      <p><a href="{{resetUrl}}">Reset Password →</a></p>
      <p>This link expires in 1 hour. If you didn't request this, you can safely ignore this email.</p>
      <p>— The {{productName}} Team</p>
    `.trim(),
    bodyText: `Password Reset\n\nHi {{displayName}},\n\nReset your password: {{resetUrl}}\n\nThis link expires in 1 hour.\n\n— The {{productName}} Team`,
    variables: ['displayName', 'productName', 'resetUrl'],
  },
  {
    id: 'email-verification',
    name: 'Verify Email',
    subject: 'Verify your {{productName}} email address',
    bodyHtml: `
      <h1>Verify Your Email</h1>
      <p>Hi {{displayName}},</p>
      <p>Please verify your email address by clicking the link below:</p>
      <p><a href="{{verifyUrl}}">Verify Email →</a></p>
      <p>This link expires in 24 hours.</p>
      <p>— The {{productName}} Team</p>
    `.trim(),
    bodyText: `Verify Your Email\n\nHi {{displayName}},\n\nVerify: {{verifyUrl}}\n\nExpires in 24 hours.\n\n— The {{productName}} Team`,
    variables: ['displayName', 'productName', 'verifyUrl'],
  },
  {
    id: 'trial-expiring',
    name: 'Trial Expiring',
    subject: 'Your {{productName}} trial ends in {{daysLeft}} days',
    bodyHtml: `
      <h1>Your trial is ending soon</h1>
      <p>Hi {{displayName}},</p>
      <p>Your {{productName}} trial ends in <strong>{{daysLeft}} days</strong>.</p>
      <p>Upgrade now to keep access to all features:</p>
      <p><a href="{{upgradeUrl}}">Upgrade →</a></p>
      <p>— The {{productName}} Team</p>
    `.trim(),
    bodyText: `Your trial ends in {{daysLeft}} days.\n\nUpgrade: {{upgradeUrl}}\n\n— The {{productName}} Team`,
    variables: ['displayName', 'productName', 'daysLeft', 'upgradeUrl'],
  },
  {
    id: 'trial-expired',
    name: 'Trial Expired',
    subject: 'Your {{productName}} trial has ended',
    bodyHtml: `
      <h1>Your trial has ended</h1>
      <p>Hi {{displayName}},</p>
      <p>Your {{productName}} trial has expired. Upgrade to continue using all features:</p>
      <p><a href="{{upgradeUrl}}">Upgrade Now →</a></p>
      <p>— The {{productName}} Team</p>
    `.trim(),
    bodyText: `Your trial has ended.\n\nUpgrade: {{upgradeUrl}}\n\n— The {{productName}} Team`,
    variables: ['displayName', 'productName', 'upgradeUrl'],
  },
  {
    id: 'invitation',
    name: 'Invitation',
    subject: "You've been invited to join {{productName}}",
    bodyHtml: `
      <h1>You're Invited!</h1>
      <p>{{inviterName}} has invited you to join {{productName}}.</p>
      <p><a href="{{inviteUrl}}">Accept Invitation →</a></p>
      <p>— The {{productName}} Team</p>
    `.trim(),
    bodyText: `{{inviterName}} invited you to {{productName}}.\n\nAccept: {{inviteUrl}}\n\n— The {{productName}} Team`,
    variables: ['inviterName', 'productName', 'inviteUrl'],
  },
  {
    id: 'payment-failed',
    name: 'Payment Failed',
    subject: 'Payment failed for {{productName}}',
    bodyHtml: `
      <h1>Payment Failed</h1>
      <p>Hi {{displayName}},</p>
      <p>We couldn't process your payment of {{amount}} {{currency}}.</p>
      <p>Please update your payment method to avoid service interruption:</p>
      <p><a href="{{billingUrl}}">Update Payment Method →</a></p>
      <p>— The {{productName}} Team</p>
    `.trim(),
    bodyText: `Payment of {{amount}} {{currency}} failed.\n\nUpdate: {{billingUrl}}\n\n— The {{productName}} Team`,
    variables: ['displayName', 'productName', 'amount', 'currency', 'billingUrl'],
  },
  {
    id: 'license-expiring',
    name: 'License Expiring',
    subject: 'Your {{productName}} license expires in {{daysLeft}} days',
    bodyHtml: `
      <h1>License Expiring Soon</h1>
      <p>Hi {{displayName}},</p>
      <p>Your {{productName}} license expires in <strong>{{daysLeft}} days</strong>.</p>
      <p>Renew now to maintain access:</p>
      <p><a href="{{renewUrl}}">Renew License →</a></p>
      <p>— The {{productName}} Team</p>
    `.trim(),
    bodyText: `License expires in {{daysLeft}} days.\n\nRenew: {{renewUrl}}\n\n— The {{productName}} Team`,
    variables: ['displayName', 'productName', 'daysLeft', 'renewUrl'],
  },
  // ── Broadcast Template ──────────────────────────────────────────
  {
    id: 'broadcast',
    name: 'Broadcast Message',
    subject: '{{subject}}',
    bodyHtml: `
      <p>Hi {{displayName}},</p>
      {{body}}
      <p>— The {{productName}} Team</p>
    `.trim(),
    bodyText: `Hi {{displayName}},\n\n{{body}}\n\n— The {{productName}} Team`,
    variables: ['displayName', 'subject', 'body', 'productName'],
  },
  // ── Diagnostics Templates ─────────────────────────────────────
  {
    id: 'diagnostics-session-created',
    name: 'Diagnostics Session Created',
    subject: 'Debug session started for your {{productName}} device',
    bodyHtml: `
      <h1>Debug Session Active</h1>
      <p>Hi {{displayName}},</p>
      <p>Our engineering team has initiated a debug session for your device to help resolve an issue you reported.</p>
      <p><strong>Session ID:</strong> {{sessionId}}<br/>
      <strong>Started:</strong> {{startedAt}}<br/>
      <strong>Duration:</strong> Up to {{maxDurationMinutes}} minutes</p>
      <p>We'll collect diagnostic data including logs and traces. No action needed on your part.</p>
      <p>Questions? Reply to this email or contact support.</p>
      <p>— The {{productName}} Engineering Team</p>
    `.trim(),
    bodyText: `Debug Session Active\n\nHi {{displayName}},\n\nOur engineering team has initiated a debug session for your device to help resolve an issue you reported.\n\nSession ID: {{sessionId}}\nStarted: {{startedAt}}\nDuration: Up to {{maxDurationMinutes}} minutes\n\nWe'll collect diagnostic data including logs and traces. No action needed on your part.\n\n— The {{productName}} Engineering Team`,
    variables: ['displayName', 'productName', 'sessionId', 'startedAt', 'maxDurationMinutes'],
  },
  {
    id: 'diagnostics-session-cancelled',
    name: 'Diagnostics Session Cancelled',
    subject: 'Debug session cancelled — {{productName}}',
    bodyHtml: `
      <h1>Debug Session Cancelled</h1>
      <p>The debug session <strong>{{sessionId}}</strong> for user {{targetUserId}} has been cancelled.</p>
      <p><strong>Cancelled by:</strong> {{cancelledBy}}<br/>
      <strong>Reason:</strong> {{reason}}<br/>
      <strong>Cancelled at:</strong> {{cancelledAt}}</p>
      <p>No further diagnostic data will be collected.</p>
      <p>— The {{productName}} Engineering Team</p>
    `.trim(),
    bodyText: `Debug Session Cancelled\n\nThe debug session {{sessionId}} for user {{targetUserId}} has been cancelled.\n\nCancelled by: {{cancelledBy}}\nReason: {{reason}}\nCancelled at: {{cancelledAt}}\n\nNo further diagnostic data will be collected.\n\n— The {{productName}} Engineering Team`,
    variables: ['productName', 'sessionId', 'targetUserId', 'cancelledBy', 'reason', 'cancelledAt'],
  },
  {
    id: 'diagnostics-session-completed',
    name: 'Diagnostics Session Completed',
    subject: 'Debug session completed — {{productName}}',
    bodyHtml: `
      <h1>Debug Session Completed</h1>
      <p>The debug session <strong>{{sessionId}}</strong> has completed.</p>
      <p><strong>Summary:</strong></p>
      <ul>
        <li>Duration: {{durationMinutes}} minutes</li>
        <li>Logs collected: {{logCount}}</li>
        <li>Traces collected: {{traceCount}}</li>
        <li>Screenshots: {{screenshotCount}}</li>
      </ul>
      <p>View full results in the admin dashboard.</p>
      <p>— The {{productName}} Engineering Team</p>
    `.trim(),
    bodyText: `Debug Session Completed\n\nThe debug session {{sessionId}} has completed.\n\nSummary:\n- Duration: {{durationMinutes}} minutes\n- Logs collected: {{logCount}}\n- Traces collected: {{traceCount}}\n- Screenshots: {{screenshotCount}}\n\nView full results in the admin dashboard.\n\n— The {{productName}} Engineering Team`,
    variables: [
      'productName',
      'sessionId',
      'durationMinutes',
      'logCount',
      'traceCount',
      'screenshotCount',
    ],
  },
  {
    id: 'diagnostics-fatal-alert',
    name: 'Diagnostics Fatal Log Alert',
    subject: '🚨 FATAL log detected — {{productName}} debug session',
    bodyHtml: `
      <h1 style="color: #dc3545;">🚨 FATAL Log Detected</h1>
      <p>A fatal error was captured during debug session <strong>{{sessionId}}</strong>.</p>
      <p><strong>Product:</strong> {{productName}}<br/>
      <strong>User:</strong> {{userId}}<br/>
      <strong>Time:</strong> {{timestamp}}<br/>
      <strong>Message:</strong> {{message}}</p>
      <p><a href="{{dashboardUrl}}">View in Dashboard →</a></p>
      <p>— The {{productName}} Alert System</p>
    `.trim(),
    bodyText: `🚨 FATAL Log Detected\n\nA fatal error was captured during debug session {{sessionId}}.\n\nProduct: {{productName}}\nUser: {{userId}}\nTime: {{timestamp}}\nMessage: {{message}}\n\nView in Dashboard: {{dashboardUrl}}\n\n— The {{productName}} Alert System`,
    variables: ['productName', 'sessionId', 'userId', 'timestamp', 'message', 'dashboardUrl'],
  },
];

/**
 * Get a built-in template by ID.
 */
export function getTemplate(templateId: string): EmailTemplate | undefined {
  return BUILT_IN_TEMPLATES.find(t => t.id === templateId);
}

/**
 * List all available template IDs.
 */
export function listTemplateIds(): string[] {
  return BUILT_IN_TEMPLATES.map(t => t.id);
}
