-- Slice 6 · ProjectStarter perms
-- Idempotent (INSERT ... WHERE NOT EXISTS).

-- indexAction (view checklist) + myQueue: for every role that could own a starter task
INSERT INTO user_groups_perms (user_group_id, controller_allowed, action_allowed, created_by, created_on)
SELECT g.c0d, 'ProjectStarter', a.act, 0, NOW()
FROM (
  SELECT 'index'    AS act UNION ALL
  SELECT 'myQueue'
) a
CROSS JOIN user_groups g
WHERE g.user_group_name IN (
    'Executive','Finance Manager','Accountant','Project Manager','Projects Coordinator',
    'Field Supervisor','Procurement','Administrator','CRM','Stores'
)
  AND NOT EXISTS (
    SELECT 1 FROM user_groups_perms p
     WHERE p.user_group_id = g.c0d
       AND p.controller_allowed = 'ProjectStarter'
       AND p.action_allowed = a.act
  );

-- Mutation endpoints: only the roles that actually own tasks (assign / complete / skip / reopen)
INSERT INTO user_groups_perms (user_group_id, controller_allowed, action_allowed, created_by, created_on)
SELECT g.c0d, 'ProjectStarter', a.act, 0, NOW()
FROM (
  SELECT 'assignOwnerAj' AS act UNION ALL
  SELECT 'completeAj'          UNION ALL
  SELECT 'skipAj'              UNION ALL
  SELECT 'reopenAj'
) a
CROSS JOIN user_groups g
WHERE g.user_group_name IN (
    'Project Manager','Projects Coordinator','Procurement','Field Supervisor','Administrator'
)
  AND NOT EXISTS (
    SELECT 1 FROM user_groups_perms p
     WHERE p.user_group_id = g.c0d
       AND p.controller_allowed = 'ProjectStarter'
       AND p.action_allowed = a.act
  );
