-- Slice 8c-Surfaces · WorksBoq / WorksPhases / WorksCrs perms
-- Idempotent: skips existing rows via NOT EXISTS.

/* ── WorksBoq (read-only viewer) ─────────────────────────────────────────── */
INSERT INTO user_groups_perms (user_group_id, controller_allowed, action_allowed, created_by, created_on)
SELECT g.c0d, 'WorksBoq', a.act, 0, NOW()
FROM (
    SELECT 'index'  AS act UNION ALL
    SELECT 'view'          UNION ALL
    SELECT 'import'
) a
CROSS JOIN user_groups g
WHERE g.user_group_name IN (
    'Projects Coordinator','Project Manager','Field Supervisor',
    'Procurement','Finance Manager','Accountant','Executive','Administrator'
)
  AND NOT EXISTS (
    SELECT 1 FROM user_groups_perms p
    WHERE p.user_group_id = g.c0d
      AND p.controller_allowed = 'WorksBoq'
      AND p.action_allowed     = a.act
);

/* ── WorksPhases · edit (PC/PM/FS/Admin) ─────────────────────────────────── */
INSERT INTO user_groups_perms (user_group_id, controller_allowed, action_allowed, created_by, created_on)
SELECT g.c0d, 'WorksPhases', a.act, 0, NOW()
FROM (
    SELECT 'index'             AS act UNION ALL
    SELECT 'plan'                     UNION ALL
    SELECT 'view'                     UNION ALL
    SELECT 'createAj'                 UNION ALL
    SELECT 'attachBoqItemAj'          UNION ALL
    SELECT 'addActivityAj'            UNION ALL
    SELECT 'markClosedAj'
) a
CROSS JOIN user_groups g
WHERE g.user_group_name IN ('Projects Coordinator','Project Manager','Field Supervisor','Administrator')
  AND NOT EXISTS (
    SELECT 1 FROM user_groups_perms p
    WHERE p.user_group_id = g.c0d
      AND p.controller_allowed = 'WorksPhases'
      AND p.action_allowed     = a.act
);

/* ── WorksPhases · read-only (Procurement/FM/Accountant/Exec) ────────────── */
INSERT INTO user_groups_perms (user_group_id, controller_allowed, action_allowed, created_by, created_on)
SELECT g.c0d, 'WorksPhases', a.act, 0, NOW()
FROM (SELECT 'index' AS act UNION ALL SELECT 'plan' UNION ALL SELECT 'view') a
CROSS JOIN user_groups g
WHERE g.user_group_name IN ('Procurement','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 = 'WorksPhases'
      AND p.action_allowed     = a.act
);

/* ── WorksCrs · authors (PC/PM/FS/Admin) ─────────────────────────────────── */
INSERT INTO user_groups_perms (user_group_id, controller_allowed, action_allowed, created_by, created_on)
SELECT g.c0d, 'WorksCrs', a.act, 0, NOW()
FROM (
    SELECT 'index'           AS act UNION ALL
    SELECT 'create'                 UNION ALL
    SELECT 'view'                   UNION ALL
    SELECT 'createAj'               UNION ALL
    SELECT 'addLineAj'              UNION ALL
    SELECT 'deleteLineAj'           UNION ALL
    SELECT 'addActivityAj'          UNION ALL
    SELECT 'submitAj'
) a
CROSS JOIN user_groups g
WHERE g.user_group_name IN ('Projects Coordinator','Project Manager','Field Supervisor','Administrator')
  AND NOT EXISTS (
    SELECT 1 FROM user_groups_perms p
    WHERE p.user_group_id = g.c0d
      AND p.controller_allowed = 'WorksCrs'
      AND p.action_allowed     = a.act
);

/* ── WorksCrs · read-only (Procurement/FM/Accountant/Exec) ───────────────── */
INSERT INTO user_groups_perms (user_group_id, controller_allowed, action_allowed, created_by, created_on)
SELECT g.c0d, 'WorksCrs', a.act, 0, NOW()
FROM (SELECT 'index' AS act UNION ALL SELECT 'create' UNION ALL SELECT 'view') a
CROSS JOIN user_groups g
WHERE g.user_group_name IN ('Procurement','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 = 'WorksCrs'
      AND p.action_allowed     = a.act
);
