
INSERT INTO roles (code, name)
SELECT 'graphics_operator', 'Opérateur graphique'
WHERE NOT EXISTS (SELECT 1 FROM roles WHERE code = 'graphics_operator');

INSERT INTO roles (code, name)
SELECT 'production_operator', 'Opérateur production'
WHERE NOT EXISTS (SELECT 1 FROM roles WHERE code = 'production_operator');

UPDATE roles
SET name = 'Administrateur maître (legacy)'
WHERE code = 'master_admin';

UPDATE roles
SET name = 'Chef d''agence (legacy)'
WHERE code = 'shop_manager';

INSERT INTO permissions (code, name)
SELECT 'users.impersonate', 'Prendre le contrôle d''un compte utilisateur'
WHERE NOT EXISTS (SELECT 1 FROM permissions WHERE code = 'users.impersonate');

INSERT INTO permissions (code, name)
SELECT 'audit.view', 'Consulter l''audit'
WHERE NOT EXISTS (SELECT 1 FROM permissions WHERE code = 'audit.view');

INSERT INTO permissions (code, name)
SELECT 'bat.manage', 'Traiter la file BAT'
WHERE NOT EXISTS (SELECT 1 FROM permissions WHERE code = 'bat.manage');

INSERT INTO permissions (code, name)
SELECT 'production.manage', 'Piloter la production'
WHERE NOT EXISTS (SELECT 1 FROM permissions WHERE code = 'production.manage');

INSERT INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id
FROM roles r
INNER JOIN permissions p ON p.code = 'backoffice.access'
WHERE r.code IN ('graphics_operator', 'production_operator')
  AND NOT EXISTS (
      SELECT 1
      FROM role_permissions rp
      WHERE rp.role_id = r.id
        AND rp.permission_id = p.id
  );

INSERT INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id
FROM roles r
INNER JOIN permissions p ON p.code = 'users.impersonate'
WHERE r.code IN ('super_admin', 'admin', 'network_manager', 'master_admin')
  AND NOT EXISTS (
      SELECT 1
      FROM role_permissions rp
      WHERE rp.role_id = r.id
        AND rp.permission_id = p.id
  );

INSERT INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id
FROM roles r
INNER JOIN permissions p ON p.code = 'audit.view'
WHERE r.code IN ('super_admin', 'admin', 'network_manager', 'master_admin')
  AND NOT EXISTS (
      SELECT 1
      FROM role_permissions rp
      WHERE rp.role_id = r.id
        AND rp.permission_id = p.id
  );

INSERT INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id
FROM roles r
INNER JOIN permissions p ON p.code = 'bat.manage'
WHERE r.code IN ('super_admin', 'admin', 'network_manager', 'graphics_operator', 'master_admin')
  AND NOT EXISTS (
      SELECT 1
      FROM role_permissions rp
      WHERE rp.role_id = r.id
        AND rp.permission_id = p.id
  );

INSERT INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id
FROM roles r
INNER JOIN permissions p ON p.code = 'production.manage'
WHERE r.code IN ('super_admin', 'admin', 'network_manager', 'production_operator', 'master_admin')
  AND NOT EXISTS (
      SELECT 1
      FROM role_permissions rp
      WHERE rp.role_id = r.id
        AND rp.permission_id = p.id
  );

INSERT INTO user_platform_roles (user_id, role_code, is_active, created_at)
SELECT upr.user_id, 'admin', upr.is_active, upr.created_at
FROM user_platform_roles upr
WHERE upr.role_code = 'master_admin'
  AND NOT EXISTS (
      SELECT 1
      FROM user_platform_roles existing
      WHERE existing.user_id = upr.user_id
        AND existing.role_code = 'admin'
  );

UPDATE user_platform_roles
SET is_active = 0
WHERE role_code = 'master_admin';

INSERT INTO user_shop_memberships (user_id, shop_id, role_code, is_active, created_at)
SELECT usm.user_id, usm.shop_id, 'agency_manager', usm.is_active, usm.created_at
FROM user_shop_memberships usm
WHERE usm.role_code = 'shop_manager'
  AND NOT EXISTS (
      SELECT 1
      FROM user_shop_memberships existing
      WHERE existing.user_id = usm.user_id
        AND existing.shop_id = usm.shop_id
        AND existing.role_code = 'agency_manager'
  );

UPDATE user_shop_memberships
SET is_active = 0
WHERE role_code = 'shop_manager';
