-- Slice 8h-GoodsScrub · GoodsScrubPack perms

/* ── Author (PC/PM/Procurement/Admin): full CRUD ─────────────────── */
INSERT INTO user_groups_perms (user_group_id, controller_allowed, action_allowed, created_by, created_on)
SELECT g.c0d, 'GoodsScrubPack', a.act, 0, NOW()
FROM (
    SELECT 'open'            AS act UNION ALL
    SELECT 'view'                    UNION ALL
    SELECT 'addLineAj'               UNION ALL
    SELECT 'removeLineAj'            UNION ALL
    SELECT 'setMaterialsUnitCostAj'  UNION ALL
    SELECT 'markCompleteAj'
) a
CROSS JOIN user_groups g
WHERE g.user_group_name IN ('Projects Coordinator','Project Manager','Procurement','Administrator')
  AND NOT EXISTS (
    SELECT 1 FROM user_groups_perms p
    WHERE p.user_group_id = g.c0d
      AND p.controller_allowed = 'GoodsScrubPack'
      AND p.action_allowed     = a.act
  );

/* ── Read-only (FM/Accountant/Executive): view scrub ─────────────── */
INSERT INTO user_groups_perms (user_group_id, controller_allowed, action_allowed, created_by, created_on)
SELECT g.c0d, 'GoodsScrubPack', 'view', 0, NOW()
FROM user_groups g
WHERE g.user_group_name IN ('Finance Manager','Accountant','Executive')
  AND NOT EXISTS (
    SELECT 1 FROM user_groups_perms p
    WHERE p.user_group_id = g.c0d
      AND p.controller_allowed = 'GoodsScrubPack'
      AND p.action_allowed     = 'view'
  );
