-- Slice 8b-3 · ServicePmPlans perms
-- Authors: PC + PM + Admin (create/edit/generate)
-- Read: Exec + FM + FS (queue visibility for the field team)
INSERT INTO user_groups_perms (user_group_id, controller_allowed, action_allowed, created_by, created_on)
SELECT g.c0d, 'ServicePmPlans', a.act, 0, NOW()
FROM (
    SELECT 'create'      AS act UNION ALL
    SELECT 'view'              UNION ALL
    SELECT 'createAj'          UNION ALL
    SELECT 'addItemAj'         UNION ALL
    SELECT 'generateAj'        UNION ALL
    SELECT 'activateAj'        UNION ALL
    SELECT 'closeAj'
) a
CROSS JOIN user_groups g
WHERE g.user_group_name IN ('Projects Coordinator','Project Manager','Administrator')
  AND NOT EXISTS (SELECT 1 FROM user_groups_perms p WHERE p.user_group_id = g.c0d AND p.controller_allowed = 'ServicePmPlans' AND p.action_allowed = a.act);

INSERT INTO user_groups_perms (user_group_id, controller_allowed, action_allowed, created_by, created_on)
SELECT g.c0d, 'ServicePmPlans', a.act, 0, NOW()
FROM (SELECT 'create' AS act UNION ALL SELECT 'view') a
CROSS JOIN user_groups g
WHERE g.user_group_name IN ('Executive','Finance Manager','Field Supervisor')
  AND NOT EXISTS (SELECT 1 FROM user_groups_perms p WHERE p.user_group_id = g.c0d AND p.controller_allowed = 'ServicePmPlans' AND p.action_allowed = a.act);
