START TRANSACTION;

INSERT IGNORE INTO permissions (code, name, module, created_at) VALUES
('settings.locations.manage', 'Manage Lokasi', 'settings', NOW()),
('settings.roles.manage', 'Manage Role', 'settings', NOW());

INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT DISTINCT rp.role_id, target.id
FROM role_permissions rp
JOIN permissions source ON source.id = rp.permission_id
JOIN permissions target ON target.code = 'settings.locations.manage'
WHERE source.code IN (
  'settings.locations.view',
  'settings.locations.add',
  'settings.locations.edit',
  'settings.branding.view',
  'settings.branding.edit'
);

INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT DISTINCT rp.role_id, target.id
FROM role_permissions rp
JOIN permissions source ON source.id = rp.permission_id
JOIN permissions target ON target.code = 'settings.roles.manage'
WHERE source.code IN (
  'settings.roles.view',
  'settings.roles.add',
  'settings.roles.edit'
);

COMMIT;
