USE emergency_tracker;

-- Add role column to users table
ALTER TABLE users 
ADD COLUMN role ENUM('user', 'team_admin', 'department_admin', 'field_admin', 'global_admin') NOT NULL DEFAULT 'user' AFTER is_admin;

-- Update existing admin users to be global_admin
UPDATE users SET role = 'global_admin' WHERE is_admin = TRUE;

-- Add role-based actions to admin_actions table
ALTER TABLE admin_actions 
MODIFY COLUMN action_type ENUM('reset_all', 'reset_password', 'reset_team', 'reset_department', 'reset_field', 'role_change', 'report_on_behalf') NOT NULL;

-- Add target_role column for role changes
ALTER TABLE admin_actions 
ADD COLUMN target_role VARCHAR(50) DEFAULT NULL AFTER target_user_id;
