-- Gmail pool for warmup recipients (no OAuth — address book only)

CREATE TABLE IF NOT EXISTS gmail_accounts (
  id INT AUTO_INCREMENT PRIMARY KEY,
  email VARCHAR(255) NOT NULL,
  display_name VARCHAR(120) NULL,
  status ENUM('active', 'inactive') NOT NULL DEFAULT 'active',
  purpose ENUM('warmup', 'inbound', 'outbound', 'all') NOT NULL DEFAULT 'warmup',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  deleted_at DATETIME NULL,
  UNIQUE KEY uq_gmail_email (email),
  INDEX idx_gmail_active (status, deleted_at, purpose)
);

INSERT IGNORE INTO permission_modules (module_key, label, module_group, sort_order) VALUES
  ('gmail_accounts', 'Gmail Accounts', 'Email', 94);

INSERT IGNORE INTO role_permissions (role_id, module_key, action_key, allowed)
SELECT wr.id, 'gmail_accounts', pa.action_key, 1
FROM workspace_roles wr
CROSS JOIN permission_actions pa
WHERE wr.slug = 'super-admin';

INSERT IGNORE INTO role_permissions (role_id, module_key, action_key, allowed)
SELECT DISTINCT rp.role_id, 'gmail_accounts', 'read', 1
FROM role_permissions rp
WHERE rp.module_key = 'warmup_mails' AND rp.action_key = 'read' AND rp.allowed = 1;
