-- Script à lancer UNE SEULE FOIS après le premier login
-- Assigne le rôle super_admin au premier user en DB (toi)
--
-- Usage : depuis le VPS
-- docker exec mysql-prod mysql -u originsdigital -p'<password>' originsdigital < assign-first-admin.sql

INSERT INTO user_roles (userId, roleId)
SELECT
  u.id,
  r.id
FROM users u
CROSS JOIN roles r
WHERE r.slug = 'super_admin'
  AND u.id = (SELECT id FROM users ORDER BY createdAt ASC LIMIT 1)
  AND NOT EXISTS (
    SELECT 1 FROM user_roles ur
    WHERE ur.userId = u.id AND ur.roleId = r.id
  );

-- Vérifie le résultat
SELECT u.nickname, u.email, r.slug as role
FROM users u
JOIN user_roles ur ON ur.userId = u.id
JOIN roles r ON r.id = ur.roleId
WHERE r.slug = 'super_admin';
