CREATE TABLE IF NOT EXISTS warmup_mail_templates (
  id INT AUTO_INCREMENT PRIMARY KEY,
  name VARCHAR(120) NOT NULL,
  subject VARCHAR(255) NOT NULL,
  body_text TEXT NOT NULL,
  status ENUM('active', 'inactive') NOT NULL DEFAULT 'active',
  sort_order INT NOT NULL DEFAULT 0,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  deleted_at DATETIME NULL,
  INDEX idx_warmup_templates_status (status, deleted_at, sort_order)
);

ALTER TABLE smtp_warmup_events
  ADD COLUMN template_id INT NULL AFTER subject,
  ADD COLUMN body_text TEXT NULL AFTER template_id,
  ADD COLUMN message_id VARCHAR(255) NULL AFTER body_text,
  ADD COLUMN placement_checked_at DATETIME NULL AFTER placement,
  ADD CONSTRAINT fk_warmup_events_template FOREIGN KEY (template_id) REFERENCES warmup_mail_templates(id) ON DELETE SET NULL;

INSERT INTO warmup_mail_templates (name, subject, body_text, status, sort_order) VALUES
  ('Quick sync', 'Quick sync on the project timeline', 'Hi — just wanted to follow up and see if you had a chance to review. Let me know when you have a moment.', 'active', 10),
  ('Following up', 'Following up on our last conversation', 'Hope your week is going well. Sharing a quick note to stay in touch.', 'active', 20),
  ('Resources', 'Resources you might find useful', 'Thanks for connecting. Looking forward to hearing your thoughts when convenient.', 'active', 30),
  ('Check-in', 'Checking in — any updates?', 'Quick check-in — happy to jump on a brief call if that helps.', 'active', 40),
  ('Proposal thoughts', 'Thoughts on the proposal', 'Wanted to share a brief update and see if you have any questions on the proposal.', 'active', 50);
