-- Step 25: Superadmin operations, recovery controls, and linked-room readiness.
-- Run after step24_room_status_fix.sql.

ALTER TABLE users
  MODIFY role ENUM('superadmin','admin','viewer','cameraman') NOT NULL DEFAULT 'viewer';

ALTER TABLE notifications
  MODIFY recipient_type ENUM('superadmin','admin','viewer','cameraman') NOT NULL;

ALTER TABLE activity_timeline
  MODIFY actor_type ENUM('superadmin','admin','viewer','cameraman','system') NOT NULL DEFAULT 'system';

CREATE TABLE IF NOT EXISTS recording_room_links (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  source_type ENUM('schedule','daily_instance','plan') NOT NULL,
  source_id INT UNSIGNED NOT NULL,
  room_id INT UNSIGNED NOT NULL,
  room_role ENUM('Primary','Linked') NOT NULL DEFAULT 'Linked',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uq_recording_room_link (source_type, source_id, room_id),
  KEY idx_recording_room_links_room (room_id),
  KEY idx_recording_room_links_source (source_type, source_id),
  CONSTRAINT fk_recording_room_links_room
    FOREIGN KEY (room_id) REFERENCES rooms(id)
    ON UPDATE CASCADE
    ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

ALTER TABLE active_recording_plans
  MODIFY status ENUM('Active','Ended','Paused','Archived') NOT NULL DEFAULT 'Active';
