-- Script to make a user an admin with all permissions
-- Replace 'your@email.com' with the actual user email

-- Step 1: Update user role to superuser
UPDATE users 
SET role = 'superuser'
WHERE email = 'your@email.com';

-- Step 2: Grant all permissions to the user
-- This will insert all permissions that the user doesn't already have

INSERT INTO user_permissions (user_id, permission_id, granted_by, granted_at, inserted_at, updated_at)
SELECT 
    u.id as user_id,
    p.id as permission_id,
    u.id as granted_by,
    NOW() as granted_at,
    NOW() as inserted_at,
    NOW() as updated_at
FROM users u
CROSS JOIN permissions p
WHERE u.email = 'your@email.com'
  AND NOT EXISTS (
    SELECT 1 FROM user_permissions up 
    WHERE up.user_id = u.id AND up.permission_id = p.id
  );

-- Step 3: Verify the results
SELECT 
    u.email,
    u.name,
    u.role,
    COUNT(up.id) as permission_count,
    (SELECT COUNT(*) FROM permissions) as total_permissions
FROM users u
LEFT JOIN user_permissions up ON up.user_id = u.id
WHERE u.email = 'your@email.com'
GROUP BY u.id, u.email, u.name, u.role;
