ALTER TABLE team_message_threads
  ADD COLUMN status_code VARCHAR(30) NOT NULL DEFAULT 'open' AFTER topic_type,
  ADD COLUMN closed_by_user_id CHAR(36) NULL AFTER created_by_user_id,
  ADD COLUMN closed_at DATETIME NULL AFTER closed_by_user_id;

ALTER TABLE team_message_threads
  ADD KEY idx_team_message_threads_team_status_last (team_id, status_code, last_message_at);

ALTER TABLE team_message_threads
  ADD CONSTRAINT fk_team_message_threads_closed_by
    FOREIGN KEY (closed_by_user_id) REFERENCES users (id);
