-- Create Super Admin Division, Department and Role if they do not exist
INSERT INTO divisions (name) SELECT 'Super Admin' WHERE NOT EXISTS (SELECT 1 FROM divisions WHERE name = 'Super Admin');

SET @division_id = (SELECT id FROM divisions WHERE name = 'Super Admin');
INSERT INTO departments (name, division_id) SELECT 'Super Admin', @division_id WHERE NOT EXISTS (SELECT 1 FROM departments WHERE name = 'Super Admin');

SET @department_id = (SELECT id FROM departments WHERE name = 'Super Admin');
INSERT INTO roles (name, department_id) SELECT 'Admin', @department_id WHERE NOT EXISTS (SELECT 1 FROM roles WHERE name = 'Admin');

-- Create Super Admin User
SET @role_id = (SELECT id FROM roles WHERE name = 'Admin' AND department_id = @department_id);
SET @hashed_password = '$2y$10$9.p6A8v.17O7.Q1yJgqjC.H9aJz/O1aB3F2aC6bF.p8aEwS2mS2W'; -- "password"

INSERT INTO users (username, password, email, division_id, department_id, role_id)
SELECT 'superadmin', @hashed_password, 'superadmin@example.com', @division_id, @department_id, @role_id
WHERE NOT EXISTS (SELECT 1 FROM users WHERE username = 'superadmin');