-- Per-account stage progress timeline

ALTER TABLE company_smtp_accounts
  ADD COLUMN warmup_start_stage_id INT NULL AFTER warmup_enabled,
  ADD COLUMN warmup_status ENUM('inactive', 'active', 'paused', 'completed', 'failed') NOT NULL DEFAULT 'inactive' AFTER warmup_day,
  ADD COLUMN warmup_completed_at DATETIME NULL AFTER warmup_status;

ALTER TABLE company_smtp_accounts
  ADD CONSTRAINT fk_warmup_start_stage FOREIGN KEY (warmup_start_stage_id) REFERENCES warmup_stages(id);

CREATE TABLE IF NOT EXISTS smtp_warmup_stage_progress (
  id BIGINT AUTO_INCREMENT PRIMARY KEY,
  account_id INT NOT NULL,
  stage_id INT NOT NULL,
  stage_number TINYINT UNSIGNED NOT NULL,
  started_at DATETIME NULL,
  ended_at DATETIME NULL,
  emails_sent INT UNSIGNED NOT NULL DEFAULT 0,
  daily_target INT UNSIGNED NULL,
  daily_target_date DATE NULL,
  status ENUM('pending', 'active', 'completed', 'failed') NOT NULL DEFAULT 'pending',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uq_account_stage (account_id, stage_number),
  INDEX idx_progress_account_active (account_id, status),
  CONSTRAINT fk_stage_progress_account FOREIGN KEY (account_id) REFERENCES company_smtp_accounts(id) ON DELETE CASCADE,
  CONSTRAINT fk_stage_progress_stage FOREIGN KEY (stage_id) REFERENCES warmup_stages(id)
);

ALTER TABLE smtp_warmup_events
  ADD COLUMN gmail_account_id INT NULL AFTER peer_account_id,
  ADD CONSTRAINT fk_warmup_events_gmail FOREIGN KEY (gmail_account_id) REFERENCES gmail_accounts(id) ON DELETE SET NULL;
