-- Pacific Logistics · slice 36 · Work Requests grants (retrofix)
--
-- The WorkRequests controller + views ship with the fleet build but the
-- perms table never got seeded, so the sidebar link never became visible
-- for anyone. This slice fixes that.
--
-- Actions the controller exposes:
--   index         — list all WRs
--   add / save    — create draft
--   view / edit / update / delete
--   submit        — driver submits a draft for review
--   cancel        — driver cancels their own
--   approve       — fleet approves
--   reject        — fleet rejects
--   startSourcing — procurement starts sourcing parts
--   markReady     — procurement / stores says parts on hand
--   convertToWo   — approved WR becomes a Work Order
--   addItem / deleteItem — line items on the WR
--
-- Role assignment:
--   Driver (25)          — index (their own), add, save, submit, cancel — mobile primarily
--   Field Supervisor (6) — full lifecycle in the field
--   Ops Manager (20)     — full
--   Dispatcher (21)      — index + view
--   Fleet Manager (22)   — full including approve/reject/convertToWo
--   Procurement (7)      — index, view, startSourcing, markReady, addItem, deleteItem
--   Administrator (10)   — everything

INSERT IGNORE INTO user_groups_perms (user_group_id, controller_allowed, action_allowed, created_by, created_on) VALUES
-- Driver (25)
(25, 'WorkRequests', 'index',    1, NOW()),
(25, 'WorkRequests', 'add',      1, NOW()),
(25, 'WorkRequests', 'save',     1, NOW()),
(25, 'WorkRequests', 'view',     1, NOW()),
(25, 'WorkRequests', 'submit',   1, NOW()),
(25, 'WorkRequests', 'cancel',   1, NOW()),
-- Field Supervisor (6)
(6,  'WorkRequests', 'index',    1, NOW()),
(6,  'WorkRequests', 'view',     1, NOW()),
(6,  'WorkRequests', 'add',      1, NOW()),
(6,  'WorkRequests', 'save',     1, NOW()),
(6,  'WorkRequests', 'edit',     1, NOW()),
(6,  'WorkRequests', 'update',   1, NOW()),
(6,  'WorkRequests', 'submit',   1, NOW()),
(6,  'WorkRequests', 'cancel',   1, NOW()),
(6,  'WorkRequests', 'reject',   1, NOW()),
(6,  'WorkRequests', 'addItem',  1, NOW()),
(6,  'WorkRequests', 'deleteItem', 1, NOW()),
-- Ops Manager (20)
(20, 'WorkRequests', 'index',    1, NOW()),
(20, 'WorkRequests', 'view',     1, NOW()),
(20, 'WorkRequests', 'add',      1, NOW()),
(20, 'WorkRequests', 'save',     1, NOW()),
(20, 'WorkRequests', 'edit',     1, NOW()),
(20, 'WorkRequests', 'update',   1, NOW()),
(20, 'WorkRequests', 'submit',   1, NOW()),
(20, 'WorkRequests', 'cancel',   1, NOW()),
(20, 'WorkRequests', 'approve',  1, NOW()),
(20, 'WorkRequests', 'reject',   1, NOW()),
(20, 'WorkRequests', 'convertToWo', 1, NOW()),
(20, 'WorkRequests', 'addItem',  1, NOW()),
(20, 'WorkRequests', 'deleteItem', 1, NOW()),
-- Dispatcher (21) — read only
(21, 'WorkRequests', 'index',    1, NOW()),
(21, 'WorkRequests', 'view',     1, NOW()),
-- Fleet Manager (22)
(22, 'WorkRequests', 'index',    1, NOW()),
(22, 'WorkRequests', 'view',     1, NOW()),
(22, 'WorkRequests', 'add',      1, NOW()),
(22, 'WorkRequests', 'save',     1, NOW()),
(22, 'WorkRequests', 'edit',     1, NOW()),
(22, 'WorkRequests', 'update',   1, NOW()),
(22, 'WorkRequests', 'submit',   1, NOW()),
(22, 'WorkRequests', 'cancel',   1, NOW()),
(22, 'WorkRequests', 'approve',  1, NOW()),
(22, 'WorkRequests', 'reject',   1, NOW()),
(22, 'WorkRequests', 'startSourcing', 1, NOW()),
(22, 'WorkRequests', 'markReady', 1, NOW()),
(22, 'WorkRequests', 'convertToWo', 1, NOW()),
(22, 'WorkRequests', 'addItem',  1, NOW()),
(22, 'WorkRequests', 'deleteItem', 1, NOW()),
(22, 'WorkRequests', 'delete',   1, NOW()),
-- Procurement (7)
(7,  'WorkRequests', 'index',    1, NOW()),
(7,  'WorkRequests', 'view',     1, NOW()),
(7,  'WorkRequests', 'startSourcing', 1, NOW()),
(7,  'WorkRequests', 'markReady', 1, NOW()),
(7,  'WorkRequests', 'addItem',  1, NOW()),
(7,  'WorkRequests', 'deleteItem', 1, NOW()),
-- Administrator (10)
(10, 'WorkRequests', 'index',    1, NOW()),
(10, 'WorkRequests', 'view',     1, NOW()),
(10, 'WorkRequests', 'add',      1, NOW()),
(10, 'WorkRequests', 'save',     1, NOW()),
(10, 'WorkRequests', 'edit',     1, NOW()),
(10, 'WorkRequests', 'update',   1, NOW()),
(10, 'WorkRequests', 'submit',   1, NOW()),
(10, 'WorkRequests', 'cancel',   1, NOW()),
(10, 'WorkRequests', 'approve',  1, NOW()),
(10, 'WorkRequests', 'reject',   1, NOW()),
(10, 'WorkRequests', 'startSourcing', 1, NOW()),
(10, 'WorkRequests', 'markReady', 1, NOW()),
(10, 'WorkRequests', 'convertToWo', 1, NOW()),
(10, 'WorkRequests', 'addItem',  1, NOW()),
(10, 'WorkRequests', 'deleteItem', 1, NOW()),
(10, 'WorkRequests', 'delete',   1, NOW());


-- Also: the Fleet section wrapper in _sidebar_administrator.php only checks
-- Trucks/Drivers/etc — if a user has ONLY WorkRequests, the whole section
-- hides even though this link would show. That's a view-side fix; the perm
-- fix here is the immediate blocker.

SELECT ug.user_group_name, COUNT(*) AS actions
  FROM user_groups_perms ugp
  JOIN user_groups ug ON ug.c0d = ugp.user_group_id
 WHERE ugp.controller_allowed = 'WorkRequests'
 GROUP BY ug.c0d, ug.user_group_name;
