-- Check table structures and add permissions
SELECT 'directus_roles structure:' as info;
\d directus_roles;

SELECT 'directus_permissions structure:' as info;
\d directus_permissions;

-- Get the admin role ID
SELECT 'Finding admin role:' as info;
SELECT id, name FROM directus_roles WHERE name = 'Administrator' OR name = 'admin';

-- Add permissions for admin role to access moderation collections
INSERT INTO directus_permissions (role, permission, collection, action)
SELECT 
    (SELECT id FROM directus_roles WHERE name = 'Administrator' LIMIT 1),
    'full',
    'moderation_flags',
    '*'
WHERE NOT EXISTS (
    SELECT 1 FROM directus_permissions 
    WHERE collection = 'moderation_flags' 
    AND role = (SELECT id FROM directus_roles WHERE name = 'Administrator' LIMIT 1)
);

INSERT INTO directus_permissions (role, permission, collection, action)
SELECT 
    (SELECT id FROM directus_roles WHERE name = 'Administrator' LIMIT 1),
    'full',
    'user_status_history',
    '*'
WHERE NOT EXISTS (
    SELECT 1 FROM directus_permissions 
    WHERE collection = 'user_status_history' 
    AND role = (SELECT id FROM directus_roles WHERE name = 'Administrator' LIMIT 1)
);

SELECT 'Permissions added for moderation collections:' as info;
SELECT role, permission, collection, action FROM directus_permissions 
WHERE collection IN ('moderation_flags', 'user_status_history');
