START TRANSACTION;

INSERT IGNORE INTO permissions (code, name, module, created_at) VALUES
('customers.manage', 'Kelola Customer', 'customers', NOW()),
('customers.import_export.manage', 'Kelola Export/Import Customer', 'customers', 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 = 'customers.manage'
WHERE source.code IN ('customers.add', 'customers.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 = 'customers.import_export.manage'
WHERE source.code IN ('customers.export', 'customers.import');

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 = 'customers.member_class.manage'
WHERE source.code IN ('member_classes.view', 'member_classes.manage');

COMMIT;
