-- Create Admin role and assign all permissions; attach to the first user if exists
INSERT IGNORE INTO roles (name) VALUES ('Admin');

-- Map all existing permissions to Admin
INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id FROM roles r CROSS JOIN permissions p WHERE r.name='Admin';

-- Optionally assign Admin to the first existing user
INSERT IGNORE INTO user_roles (user_id, role_id)
SELECT (SELECT MIN(id) FROM users), (SELECT id FROM roles WHERE name='Admin')
WHERE (SELECT COUNT(*) FROM users) > 0;
