-- email_templates: Admin-editable predefined templates
CREATE TABLE email_templates (
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
  template_key VARCHAR(50) NOT NULL UNIQUE CHECK (template_key IN (
    'maintenance_due_soon', 'maintenance_overdue',
    'document_expiring', 'document_expired'
  )),
  name VARCHAR(100) NOT NULL,
  description TEXT,
  subject VARCHAR(255) NOT NULL,
  body TEXT NOT NULL,
  variables JSONB DEFAULT '[]'::jsonb,
  is_active BOOLEAN DEFAULT true,
  created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

-- notification_logs: Track sent notifications
CREATE TABLE notification_logs (
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
  user_id VARCHAR(255) NOT NULL,
  notification_type VARCHAR(20) NOT NULL CHECK (notification_type IN ('email', 'toast')),
  template_key VARCHAR(50) NOT NULL,
  recipient_email VARCHAR(255),
  subject VARCHAR(255),
  reference_type VARCHAR(50),  -- 'maintenance_schedule' or 'document'
  reference_id UUID,
  status VARCHAR(20) DEFAULT 'sent' CHECK (status IN ('pending', 'sent', 'failed')),
  error_message TEXT,
  sent_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
);

-- Indexes
CREATE INDEX idx_notification_logs_user_id ON notification_logs(user_id);
CREATE INDEX idx_notification_logs_reference ON notification_logs(reference_type, reference_id);
CREATE INDEX idx_notification_logs_sent_at ON notification_logs(sent_at DESC);

-- Seed 4 default templates
INSERT INTO email_templates (template_key, name, description, subject, body, variables) VALUES
  ('maintenance_due_soon', 'Maintenance Due Soon', 'Sent when maintenance is due within 30 days or 500 miles',
   'MotoVaultPro: Maintenance Due Soon for {{vehicleName}}',
   'Hi {{userName}},

Your {{category}} maintenance for {{vehicleName}} is due soon.

Due Date: {{dueDate}}
Due Mileage: {{dueMileage}} miles
Items: {{subtypes}}

Best regards,
MotoVaultPro',
   '["userName", "vehicleName", "category", "subtypes", "dueDate", "dueMileage"]'),
  ('maintenance_overdue', 'Maintenance Overdue', 'Sent when maintenance is past due',
   'MotoVaultPro: OVERDUE Maintenance for {{vehicleName}}',
   'Hi {{userName}},

Your {{category}} maintenance for {{vehicleName}} is OVERDUE.

Was Due: {{dueDate}}
Was Due At: {{dueMileage}} miles
Items: {{subtypes}}

Please schedule service as soon as possible.

Best regards,
MotoVaultPro',
   '["userName", "vehicleName", "category", "subtypes", "dueDate", "dueMileage"]'),
  ('document_expiring', 'Document Expiring Soon', 'Sent when document expires within 30 days',
   'MotoVaultPro: {{documentTitle}} Expiring Soon',
   'Hi {{userName}},

Your {{documentType}} document "{{documentTitle}}" for {{vehicleName}} is expiring soon.

Expiration Date: {{expirationDate}}

Please renew before expiration.

Best regards,
MotoVaultPro',
   '["userName", "vehicleName", "documentType", "documentTitle", "expirationDate"]'),
  ('document_expired', 'Document Expired', 'Sent when document has expired',
   'MotoVaultPro: {{documentTitle}} Has EXPIRED',
   'Hi {{userName}},

Your {{documentType}} document "{{documentTitle}}" for {{vehicleName}} has EXPIRED.

Expired On: {{expirationDate}}

Please renew immediately.

Best regards,
MotoVaultPro',
   '["userName", "vehicleName", "documentType", "documentTitle", "expirationDate"]');
