-- Adds support for operators refusing an incoming chat, and a session status for when
-- every online operator has refused (or gone away) — the visitor gets told no one's
-- available and is routed to leave a message instead.

ALTER TABLE chat_sessions MODIFY status ENUM('waiting','active','closed','no_agent') NOT NULL DEFAULT 'waiting';

CREATE TABLE IF NOT EXISTS chat_session_refusals (
  session_id INT UNSIGNED NOT NULL,
  operator_id INT UNSIGNED NOT NULL,
  refused_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (session_id, operator_id),
  FOREIGN KEY (session_id) REFERENCES chat_sessions(id) ON DELETE CASCADE,
  FOREIGN KEY (operator_id) REFERENCES chat_operators(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
