CREATE TABLE IF NOT EXISTS dispatch_web_sessions (
  id INT AUTO_INCREMENT PRIMARY KEY,
  dispatcher_user_id INT NOT NULL,
  dispatcher_name VARCHAR(128) DEFAULT '',
  status VARCHAR(32) DEFAULT 'ACTIVE',
  started_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  ended_at DATETIME NULL,
  INDEX(dispatcher_user_id),
  INDEX(status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS dispatch_web_notes (
  id INT AUTO_INCREMENT PRIMARY KEY,
  call_id INT NOT NULL,
  dispatcher_user_id INT NOT NULL,
  dispatcher_name VARCHAR(128) DEFAULT '',
  note_text TEXT,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  INDEX(call_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT IGNORE INTO role_permissions(role,permission) VALUES
('dispatcher','view_cad'),
('dispatcher','create_call'),
('dispatcher','update_unit'),
('dispatcher','dispatch_web'),
('officer','dispatch_web'),
('admin','dispatch_web');

INSERT IGNORE INTO commands(code,title,description,permission,action) VALUES
('DWEB','Web Dispatch','Open web-only dispatch console','dispatch_web','dispatch_web');
