WITH canonical_menu_items ("systemKey", "label", "itemType", "routeOrUrl", "icon", "displayOrder", "isRequired", "isActive") AS (
  VALUES
    ('dashboard', 'Dashboard', 'INTERNAL_PAGE'::"MenuItemType", '/', 'LayoutDashboard', 10, true, true),
    ('fleet', 'Fleet', 'INTERNAL_PAGE'::"MenuItemType", '/fleet', 'Car', 20, true, true),
    ('reservations', 'Booking', 'INTERNAL_PAGE'::"MenuItemType", '/reservations', 'Calendar', 30, true, true),
    ('customers', 'Customers', 'INTERNAL_PAGE'::"MenuItemType", '/customers', 'Users', 40, false, true),
    ('subscription', 'Subscription', 'INTERNAL_PAGE'::"MenuItemType", '/subscription', 'CreditCard', 50, true, true),
    ('contracts', 'Contracts', 'INTERNAL_PAGE'::"MenuItemType", '/contracts', 'FileText', 60, false, true),
    ('notifications', 'Notifications', 'INTERNAL_PAGE'::"MenuItemType", '/notifications', 'Bell', 70, false, true),
    ('settings', 'Settings', 'INTERNAL_PAGE'::"MenuItemType", '/settings', 'Settings', 80, false, true),
    ('onlineReservations', 'Online Reservations', 'INTERNAL_PAGE'::"MenuItemType", '/online-reservations', 'Globe', 90, false, true),
    ('team', 'Team', 'INTERNAL_PAGE'::"MenuItemType", '/team', 'UserPlus', 100, false, true),
    ('offers', 'Offers', 'INTERNAL_PAGE'::"MenuItemType", '/offers', 'Tag', 110, false, true),
    ('reports', 'Reports', 'INTERNAL_PAGE'::"MenuItemType", '/reports', 'BarChart2', 120, false, true),
    ('billing', 'Customer Billing', 'INTERNAL_PAGE'::"MenuItemType", '/billing', 'CreditCard', 130, false, true),
    ('reviews', 'Reviews', 'INTERNAL_PAGE'::"MenuItemType", '/reviews', 'Star', 140, false, true),
    ('complaints', 'Complaints', 'INTERNAL_PAGE'::"MenuItemType", '/complaints', 'AlertTriangle', 150, false, true)
)
INSERT INTO "menu_items" (
  "id",
  "systemKey",
  "label",
  "itemType",
  "routeOrUrl",
  "icon",
  "displayOrder",
  "openInNewTab",
  "isRequired",
  "isActive",
  "createdBy",
  "updatedBy"
)
SELECT
  'menu_' || "systemKey",
  "systemKey",
  "label",
  "itemType",
  "routeOrUrl",
  "icon",
  "displayOrder",
  false,
  "isRequired",
  "isActive",
  'system',
  'system'
FROM canonical_menu_items
ON CONFLICT ("systemKey") DO UPDATE
SET
  "label" = EXCLUDED."label",
  "itemType" = EXCLUDED."itemType",
  "routeOrUrl" = EXCLUDED."routeOrUrl",
  "icon" = EXCLUDED."icon",
  "displayOrder" = EXCLUDED."displayOrder",
  "isRequired" = EXCLUDED."isRequired",
  "isActive" = EXCLUDED."isActive",
  "updatedBy" = 'system',
  "updatedAt" = CURRENT_TIMESTAMP;

DELETE FROM "subscription_menu_items"
WHERE "menuItemId" IN (
  SELECT "id"
  FROM "menu_items"
  WHERE "systemKey" IN (
    'dashboard',
    'fleet',
    'reservations',
    'customers',
    'subscription',
    'contracts',
    'notifications',
    'settings',
    'onlineReservations',
    'team',
    'offers',
    'reports',
    'billing',
    'reviews',
    'complaints'
  )
);

WITH canonical_subscription_assignments ("systemKey", "plan", "displayOrder") AS (
  VALUES
    ('dashboard', 'STARTER'::"Plan", 10),
    ('fleet', 'STARTER'::"Plan", 20),
    ('reservations', 'STARTER'::"Plan", 30),
    ('customers', 'STARTER'::"Plan", 40),
    ('subscription', 'STARTER'::"Plan", 50),
    ('contracts', 'STARTER'::"Plan", 60),
    ('notifications', 'STARTER'::"Plan", 70),
    ('settings', 'STARTER'::"Plan", 80),

    ('dashboard', 'GROWTH'::"Plan", 10),
    ('fleet', 'GROWTH'::"Plan", 20),
    ('reservations', 'GROWTH'::"Plan", 30),
    ('customers', 'GROWTH'::"Plan", 40),
    ('subscription', 'GROWTH'::"Plan", 50),
    ('contracts', 'GROWTH'::"Plan", 60),
    ('notifications', 'GROWTH'::"Plan", 70),
    ('settings', 'GROWTH'::"Plan", 80),
    ('onlineReservations', 'GROWTH'::"Plan", 90),
    ('team', 'GROWTH'::"Plan", 100),
    ('offers', 'GROWTH'::"Plan", 110),
    ('reports', 'GROWTH'::"Plan", 120),
    ('billing', 'GROWTH'::"Plan", 130),
    ('reviews', 'GROWTH'::"Plan", 140),
    ('complaints', 'GROWTH'::"Plan", 150),

    ('dashboard', 'PRO'::"Plan", 10),
    ('fleet', 'PRO'::"Plan", 20),
    ('reservations', 'PRO'::"Plan", 30),
    ('customers', 'PRO'::"Plan", 40),
    ('subscription', 'PRO'::"Plan", 50),
    ('contracts', 'PRO'::"Plan", 60),
    ('notifications', 'PRO'::"Plan", 70),
    ('settings', 'PRO'::"Plan", 80),
    ('onlineReservations', 'PRO'::"Plan", 90),
    ('team', 'PRO'::"Plan", 100),
    ('offers', 'PRO'::"Plan", 110),
    ('reports', 'PRO'::"Plan", 120),
    ('billing', 'PRO'::"Plan", 130),
    ('reviews', 'PRO'::"Plan", 140),
    ('complaints', 'PRO'::"Plan", 150)
)
INSERT INTO "subscription_menu_items" (
  "id",
  "plan",
  "menuItemId",
  "displayOrder",
  "isActive"
)
SELECT
  'menu_sub_' || lower(csa."plan"::text) || '_' || csa."systemKey",
  csa."plan",
  mi."id",
  csa."displayOrder",
  true
FROM canonical_subscription_assignments csa
JOIN "menu_items" mi ON mi."systemKey" = csa."systemKey";

DELETE FROM "menu_item_role_visibility"
WHERE "menuItemId" IN (
  SELECT "id"
  FROM "menu_items"
  WHERE "systemKey" IN (
    'dashboard',
    'fleet',
    'reservations',
    'customers',
    'subscription',
    'contracts',
    'notifications',
    'settings',
    'onlineReservations',
    'team',
    'offers',
    'reports',
    'billing',
    'reviews',
    'complaints'
  )
);

WITH canonical_role_visibilities ("systemKey", "role") AS (
  VALUES
    ('dashboard', 'OWNER'::"EmployeeRole"),
    ('dashboard', 'MANAGER'::"EmployeeRole"),
    ('dashboard', 'AGENT'::"EmployeeRole"),
    ('fleet', 'OWNER'::"EmployeeRole"),
    ('fleet', 'MANAGER'::"EmployeeRole"),
    ('fleet', 'AGENT'::"EmployeeRole"),
    ('reservations', 'OWNER'::"EmployeeRole"),
    ('reservations', 'MANAGER'::"EmployeeRole"),
    ('reservations', 'AGENT'::"EmployeeRole"),
    ('customers', 'OWNER'::"EmployeeRole"),
    ('customers', 'MANAGER'::"EmployeeRole"),
    ('customers', 'AGENT'::"EmployeeRole"),
    ('subscription', 'OWNER'::"EmployeeRole"),
    ('contracts', 'OWNER'::"EmployeeRole"),
    ('contracts', 'MANAGER'::"EmployeeRole"),
    ('contracts', 'AGENT'::"EmployeeRole"),
    ('notifications', 'OWNER'::"EmployeeRole"),
    ('notifications', 'MANAGER'::"EmployeeRole"),
    ('notifications', 'AGENT'::"EmployeeRole"),
    ('settings', 'OWNER'::"EmployeeRole"),
    ('onlineReservations', 'OWNER'::"EmployeeRole"),
    ('onlineReservations', 'MANAGER'::"EmployeeRole"),
    ('onlineReservations', 'AGENT'::"EmployeeRole"),
    ('team', 'OWNER'::"EmployeeRole"),
    ('team', 'MANAGER'::"EmployeeRole"),
    ('team', 'AGENT'::"EmployeeRole"),
    ('offers', 'OWNER'::"EmployeeRole"),
    ('offers', 'MANAGER'::"EmployeeRole"),
    ('reports', 'OWNER'::"EmployeeRole"),
    ('reports', 'MANAGER'::"EmployeeRole"),
    ('billing', 'OWNER'::"EmployeeRole"),
    ('billing', 'MANAGER'::"EmployeeRole"),
    ('reviews', 'OWNER'::"EmployeeRole"),
    ('reviews', 'MANAGER'::"EmployeeRole"),
    ('reviews', 'AGENT'::"EmployeeRole"),
    ('complaints', 'OWNER'::"EmployeeRole"),
    ('complaints', 'MANAGER'::"EmployeeRole"),
    ('complaints', 'AGENT'::"EmployeeRole")
)
INSERT INTO "menu_item_role_visibility" (
  "id",
  "menuItemId",
  "role"
)
SELECT
  'menu_role_' || lower(crv."role"::text) || '_' || crv."systemKey",
  mi."id",
  crv."role"
FROM canonical_role_visibilities crv
JOIN "menu_items" mi ON mi."systemKey" = crv."systemKey";
