-- 085_hr_manage_package.sql
-- Satukan flow aktif HR & Payroll admin/internal ke hr.manage

INSERT INTO permissions (code, name, module, created_at)
SELECT 'hr.manage', 'Kelola HR & Payroll', 'hr', NOW()
WHERE NOT EXISTS (
  SELECT 1 FROM permissions WHERE code = 'hr.manage'
);

UPDATE permissions
SET name = 'Kelola HR & Payroll',
    module = 'hr'
WHERE code = 'hr.manage';

INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT DISTINCT rp.role_id, p_manage.id
FROM role_permissions rp
JOIN permissions p_old ON p_old.id = rp.permission_id
JOIN permissions p_manage ON p_manage.code = 'hr.manage'
WHERE p_old.code IN ('hr.view', 'payroll.view');
