/* =========================================================
   Sprint 4 - POS Settings permissions
   - Settings -> POS (rekening + QRIS)

   NOTE:
   Schema permissions di project ini pakai kolom:
   - code, name, module, created_at
   (bukan group_key/is_system)
   ========================================================= */

START TRANSACTION;

INSERT INTO permissions (code, name, module, created_at) VALUES
  ('settings.pos.view', 'Lihat Pengaturan POS', 'SETTINGS', NOW()),
  ('settings.pos.edit', 'Edit Pengaturan POS', 'SETTINGS', NOW())
ON DUPLICATE KEY UPDATE
  name=VALUES(name),
  module=VALUES(module);

/* role mapping (optional default) */
SET @role_owner       := (SELECT id FROM roles WHERE code='owner' LIMIT 1);
SET @role_admin       := (SELECT id FROM roles WHERE code='admin' LIMIT 1);
SET @role_kepala_toko := (SELECT id FROM roles WHERE code='kepala_toko' LIMIT 1);

INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT @role_owner, p.id FROM permissions p WHERE p.code IN ('settings.pos.view','settings.pos.edit');

INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT @role_admin, p.id FROM permissions p WHERE p.code IN ('settings.pos.view','settings.pos.edit');

INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT @role_kepala_toko, p.id FROM permissions p WHERE p.code IN ('settings.pos.view');

COMMIT;
