CREATE TABLE IF NOT EXISTS broadcast_campaigns (
  id INT AUTO_INCREMENT PRIMARY KEY,
  title VARCHAR(255) NOT NULL,
  message_text TEXT NOT NULL,
  segment VARCHAR(80) NOT NULL DEFAULT 'all',
  model_name VARCHAR(255) NULL,
  days_filter INT NULL,
  total_recipients INT NOT NULL DEFAULT 0,
  sent_success INT NOT NULL DEFAULT 0,
  sent_failed INT NOT NULL DEFAULT 0,
  status VARCHAR(50) NOT NULL DEFAULT 'draft',
  created_at DATETIME NOT NULL,
  started_at DATETIME NULL,
  finished_at DATETIME NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS broadcast_logs (
  id INT AUTO_INCREMENT PRIMARY KEY,
  campaign_id INT NOT NULL,
  chat_id BIGINT NOT NULL,
  user_name VARCHAR(255) NULL,
  success TINYINT(1) NOT NULL DEFAULT 0,
  response_text TEXT NULL,
  sent_at DATETIME NOT NULL,
  UNIQUE KEY uniq_campaign_chat (campaign_id, chat_id),
  INDEX idx_campaign (campaign_id),
  INDEX idx_chat (chat_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
