/**
 * Migration: Add subscription tier change email template
 * @ai-summary Adds email template for subscription tier change notifications
 * @ai-context Template sent when tier changes via admin, user action, or grace period expiration
 */

-- Extend template_key CHECK constraint to include subscription_tier_change
ALTER TABLE email_templates
DROP CONSTRAINT IF EXISTS email_templates_template_key_check;

ALTER TABLE email_templates
ADD CONSTRAINT email_templates_template_key_check
CHECK (template_key IN (
  'maintenance_due_soon', 'maintenance_overdue',
  'document_expiring', 'document_expired',
  'payment_failed_immediate', 'payment_failed_7day', 'payment_failed_1day',
  'subscription_tier_change'
));

-- Insert or update subscription tier change email template
INSERT INTO email_templates (template_key, name, description, subject, body, variables, html_body) VALUES
  (
    'subscription_tier_change',
    'Subscription Tier Change',
    'Sent when user subscription tier changes (upgrade, downgrade, or admin override)',
    'MotoVaultPro: Your Subscription Has Been {{changeType}}',
    'Hi {{userName}},

Your MotoVaultPro subscription has been {{changeType}}.

Previous Tier: {{previousTier}}
New Tier: {{newTier}}
Reason: {{reason}}

{{additionalInfo}}

If you have any questions, please contact support.

Best regards,
MotoVaultPro Team',
    '["userName", "changeType", "previousTier", "newTier", "reason", "additionalInfo"]',
    '<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Subscription Tier Change</title>
</head>
<body style="margin: 0; padding: 0; font-family: Arial, sans-serif; background-color: #f4f4f4;">
  <table width="100%" cellpadding="0" cellspacing="0" style="background-color: #f4f4f4; padding: 20px;">
    <tr>
      <td align="center">
        <table width="600" cellpadding="0" cellspacing="0" style="background-color: #ffffff; border-radius: 8px; overflow: hidden; box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
          <tr>
            <td style="background-color: #1976d2; padding: 30px; text-align: center;">
              <h1 style="color: #ffffff; margin: 0; font-size: 24px;">Subscription {{changeType}}</h1>
            </td>
          </tr>
          <tr>
            <td style="padding: 40px 30px;">
              <p style="color: #333333; font-size: 16px; line-height: 1.6; margin: 0 0 20px 0;">Hi {{userName}},</p>
              <p style="color: #333333; font-size: 16px; line-height: 1.6; margin: 0 0 20px 0;">Your MotoVaultPro subscription has been <strong>{{changeType}}</strong>.</p>
              <table cellpadding="0" cellspacing="0" style="margin: 20px 0; width: 100%;">
                <tr>
                  <td style="padding: 15px; background-color: #f9f9f9; border-left: 4px solid #1976d2;">
                    <p style="margin: 0 0 10px 0; color: #666666; font-size: 14px;"><strong>Previous Tier:</strong> {{previousTier}}</p>
                    <p style="margin: 0 0 10px 0; color: #666666; font-size: 14px;"><strong>New Tier:</strong> {{newTier}}</p>
                    <p style="margin: 0; color: #666666; font-size: 14px;"><strong>Reason:</strong> {{reason}}</p>
                  </td>
                </tr>
              </table>
              <p style="color: #333333; font-size: 16px; line-height: 1.6; margin: 20px 0;">If you have any questions about this change, please contact our support team.</p>
              <div style="text-align: center; margin: 30px 0;">
                <a href="https://motovaultpro.com/settings/billing" style="display: inline-block; padding: 14px 28px; background-color: #1976d2; color: #ffffff; text-decoration: none; border-radius: 4px; font-weight: bold;">View Subscription</a>
              </div>
            </td>
          </tr>
          <tr>
            <td style="padding: 20px 30px; background-color: #f9f9f9; border-top: 1px solid #e0e0e0;">
              <p style="color: #666666; font-size: 14px; line-height: 1.6; margin: 0;">Best regards,<br>MotoVaultPro Team</p>
            </td>
          </tr>
        </table>
      </td>
    </tr>
  </table>
</body>
</html>'
  )
ON CONFLICT (template_key) DO UPDATE SET
  name = EXCLUDED.name,
  description = EXCLUDED.description,
  subject = EXCLUDED.subject,
  body = EXCLUDED.body,
  variables = EXCLUDED.variables,
  html_body = EXCLUDED.html_body,
  updated_at = NOW();
