-- Seed module × CRUD permissions (view / add / edit / delete)

INSERT INTO permissions (code, name, module) VALUES
('master.view', 'View Master Data', 'master'),
('master.add', 'Add Master Data', 'master'),
('master.edit', 'Edit Master Data', 'master'),
('master.delete', 'Delete Master Data', 'master'),
('cost.view', 'View Cost Engine', 'cost'),
('cost.add', 'Add Cost Engine', 'cost'),
('cost.edit', 'Edit Cost Engine', 'cost'),
('cost.delete', 'Delete Cost Engine', 'cost'),
('costing_op.view', 'View Costing OP', 'costing_op'),
('costing_op.add', 'Add Costing OP', 'costing_op'),
('costing_op.edit', 'Edit Costing OP', 'costing_op'),
('costing_op.delete', 'Delete Costing OP', 'costing_op'),
('bom.view', 'View BOM Management', 'bom'),
('bom.add', 'Add BOM Management', 'bom'),
('bom.edit', 'Edit BOM Management', 'bom'),
('bom.delete', 'Delete BOM Management', 'bom'),
('mrp.view', 'View MRP Management', 'mrp'),
('mrp.add', 'Add MRP Management', 'mrp'),
('mrp.edit', 'Edit MRP Management', 'mrp'),
('mrp.delete', 'Delete MRP Management', 'mrp'),
('purchase.view', 'View Purchase Management', 'purchase'),
('purchase.add', 'Add Purchase Management', 'purchase'),
('purchase.edit', 'Edit Purchase Management', 'purchase'),
('purchase.delete', 'Delete Purchase Management', 'purchase'),
('inventory.view', 'View Inventory Management', 'inventory'),
('inventory.add', 'Add Inventory Management', 'inventory'),
('inventory.edit', 'Edit Inventory Management', 'inventory'),
('inventory.delete', 'Delete Inventory Management', 'inventory'),
('production.view', 'View Production Management', 'production'),
('production.add', 'Add Production Management', 'production'),
('production.edit', 'Edit Production Management', 'production'),
('production.delete', 'Delete Production Management', 'production'),
('quality.view', 'View Quality Management', 'quality'),
('quality.add', 'Add Quality Management', 'quality'),
('quality.edit', 'Edit Quality Management', 'quality'),
('quality.delete', 'Delete Quality Management', 'quality'),
('hr.view', 'View HR Management', 'hr'),
('hr.add', 'Add HR Management', 'hr'),
('hr.edit', 'Edit HR Management', 'hr'),
('hr.delete', 'Delete HR Management', 'hr'),
('accounting.view', 'View Accounting Management', 'accounting'),
('accounting.add', 'Add Accounting Management', 'accounting'),
('accounting.edit', 'Edit Accounting Management', 'accounting'),
('accounting.delete', 'Delete Accounting Management', 'accounting'),
('shipment.view', 'View Shipment Management', 'shipment'),
('shipment.add', 'Add Shipment Management', 'shipment'),
('shipment.edit', 'Edit Shipment Management', 'shipment'),
('shipment.delete', 'Delete Shipment Management', 'shipment'),
('reports.view', 'View Reports Management', 'reports'),
('reports.add', 'Add Reports Management', 'reports'),
('reports.edit', 'Edit Reports Management', 'reports'),
('reports.delete', 'Delete Reports Management', 'reports'),
('users.view', 'View User Permissions', 'users'),
('users.add', 'Add User Permissions', 'users'),
('users.edit', 'Edit User Permissions', 'users'),
('users.delete', 'Delete User Permissions', 'users')
ON DUPLICATE KEY UPDATE name = VALUES(name), module = VALUES(module);

-- Grant all CRUD permissions to system ADMIN roles
INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id
FROM roles r
CROSS JOIN permissions p
WHERE r.code = 'ADMIN'
  AND r.is_system = 1
  AND (
      p.code LIKE '%.view'
      OR p.code LIKE '%.add'
      OR p.code LIKE '%.edit'
      OR p.code LIKE '%.delete'
  );
