-- =========================================================
-- 048_dashboard_dept_contribution_permission.sql
-- Sprint: Dashboard – Kontribusi Departemen Produksi panel
--
-- Adds the permission that gates the dept contribution panel.
-- Also assigns it to the owner role by default.
-- =========================================================

INSERT IGNORE INTO permissions (code, name, module, created_at) VALUES
  (
    'dashboard.view_dept_contribution',
    'Lihat Kontribusi Departemen Produksi',
    'dashboard',
    NOW()
  );

-- Assign to privileged built-in roles.
-- Safe: uses existing roles columns only and INSERT IGNORE prevents duplicates.
INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT r.id, p.id
FROM roles r
JOIN permissions p ON p.code = 'dashboard.view_dept_contribution'
WHERE r.code IN ('owner', 'admin', 'superadmin', 'kepala_toko')
   OR r.name IN ('Owner', 'Admin', 'Superadmin', 'Kepala Toko');
