-- EAF Delays Pareto — route update only.
-- The new screen reuses the existing IHDelays2.WebApp stored procedures
-- (GetDelaysTimeSummary, GetDelayCategoriesStacked, GetProductionItems, GetShifts,
--  GetDelayCategories), so no SP changes are needed.
--
-- IMPORTANT — the legacy "Delays Pareto" screen has a tangled route setup:
--   * Two FrontEnd.Routes rows share ModulePath 'custom-screens/delays/delays-4/':
--       - one with ModuleName 'delays-3'  (stale / mislabeled, but it IS the row the menu points to)
--       - one with ModuleName 'delays-4'
--     Both rows carry the SAME [Route] '!/delays/delays4', so dispatch only works by the
--     last-wins duplicate-route coincidence.
--   * The "Delays Pareto" menu items link (FrontEnd.Menu.RouteID, FK -> Routes.Id) to the
--     'delays-3' row, NOT the 'delays-4' row.
--
-- So we must repoint the row the MENU actually references. Driving the UPDATE off FrontEnd.Menu
-- makes this correct regardless of Id values or the delays-3/delays-4 mislabeling across
-- environments. Updating the row in place keeps the menu item linked (FK preserved); it just
-- starts resolving to the new screen.
USE [IHBoxSystem]
GO

---- Inspect first (optional): see exactly which route row the menu links to.
-- SELECT m.Id AS MenuId, m.Name AS MenuName, r.Id AS RouteId, r.[Route], r.ModuleName, r.ModulePath
-- FROM FrontEnd.Menu m
-- JOIN FrontEnd.Routes r ON r.Id = m.RouteID
-- WHERE m.Name = 'Delays Pareto';

---- Change route: legacy custom-screens/delays/delays-4 -> pages/eaf/delays-pareto.
---- Targets the Routes row(s) referenced by the "Delays Pareto" menu items.
UPDATE r
    SET r.ModulePath = 'pages/eaf/delays-pareto/',
        r.ModuleName = 'delays-pareto',
        r.[Route]    = '!/delays-pareto(/:start)(/:end)(/:machineId)(/:shiftId)(/:crewId)(/:delayCategoryId)(/)'
FROM FrontEnd.Routes r
WHERE r.ModulePath = 'custom-screens/delays/delays-4/'
  AND r.Id IN (SELECT DISTINCT m.RouteID FROM FrontEnd.Menu m WHERE m.Name = 'Delays Pareto')
GO

---- Leftover (no action): the second 'delays-4' row keeps [Route] '!/delays/delays4' pointing at
---- the legacy screen folder. No menu references it; it remains reachable only by the old URL and
---- serves as the untouched legacy reference. Clean it up separately if desired.
