-- Seed roles and permissions for CMR (run after schema.sql)

USE cmr_ecommerce;

INSERT IGNORE INTO roles (name, description) VALUES
('Admin', 'Full system access'),
('POS User', 'Point of sale and sales'),
('Inventory Manager', 'Inventory, ingredients, purchases'),
('Production Manager', 'Manufacturing and production'),
('Finance', 'Accounting, reports, payments');

INSERT IGNORE INTO permissions (name, description) VALUES
('dashboard', 'View dashboard'),
('users', 'User management'),
('customers', 'Customer management'),
('suppliers', 'Supplier management'),
('products', 'Product management'),
('ingredients', 'Ingredient management'),
('recipes', 'Recipe management'),
('pos', 'Point of sale'),
('sales', 'Sales and invoices'),
('purchases', 'Purchases'),
('production', 'Production / manufacturing'),
('reports', 'Reports'),
('accounting', 'Accounting and ledger'),
('store', 'Online store settings'),
('payroll', 'Payroll and employees');

-- Grant Admin all permissions
INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id FROM roles r CROSS JOIN permissions p WHERE r.name = 'Admin';

-- Grant other roles their areas
INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id FROM roles r JOIN permissions p ON p.name IN ('dashboard','pos','sales') WHERE r.name = 'POS User';

INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id FROM roles r JOIN permissions p ON p.name IN ('dashboard','customers','suppliers','products','ingredients','recipes','purchases') WHERE r.name = 'Inventory Manager';

INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id FROM roles r JOIN permissions p ON p.name IN ('dashboard','ingredients','recipes','production') WHERE r.name = 'Production Manager';

INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id FROM roles r JOIN permissions p ON p.name IN ('dashboard','customers','suppliers','sales','purchases','reports','accounting','payroll') WHERE r.name = 'Finance';
