UPDATE "menu_items"
SET "systemKey" = 'online-reservations',
    "updatedBy" = 'system',
    "updatedAt" = CURRENT_TIMESTAMP
WHERE "systemKey" = 'onlineReservations'
  AND NOT EXISTS (
    SELECT 1 FROM "menu_items" WHERE "systemKey" = 'online-reservations'
  );

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

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

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

    ('dashboard', 'GROWTH'::"Plan", 10),
    ('reservations', 'GROWTH'::"Plan", 20),
    ('fleet', 'GROWTH'::"Plan", 30),
    ('customers', 'GROWTH'::"Plan", 40),
    ('reports', 'GROWTH'::"Plan", 50),
    ('billing', 'GROWTH'::"Plan", 60),
    ('settings', 'GROWTH'::"Plan", 70),
    ('online-reservations', 'GROWTH'::"Plan", 80),
    ('offers', 'GROWTH'::"Plan", 90),
    ('team', 'GROWTH'::"Plan", 100),
    ('contracts', 'GROWTH'::"Plan", 110),
    ('notifications', 'GROWTH'::"Plan", 120),

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

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

WITH canonical_role_visibilities ("systemKey", "role") AS (
  VALUES
    ('dashboard', 'OWNER'::"EmployeeRole"),
    ('dashboard', 'MANAGER'::"EmployeeRole"),
    ('dashboard', 'AGENT'::"EmployeeRole"),
    ('reservations', 'OWNER'::"EmployeeRole"),
    ('reservations', 'MANAGER'::"EmployeeRole"),
    ('reservations', 'AGENT'::"EmployeeRole"),
    ('fleet', 'OWNER'::"EmployeeRole"),
    ('fleet', 'MANAGER'::"EmployeeRole"),
    ('fleet', 'AGENT'::"EmployeeRole"),
    ('customers', 'OWNER'::"EmployeeRole"),
    ('customers', 'MANAGER'::"EmployeeRole"),
    ('customers', 'AGENT'::"EmployeeRole"),
    ('reports', 'OWNER'::"EmployeeRole"),
    ('reports', 'MANAGER'::"EmployeeRole"),
    ('billing', 'OWNER'::"EmployeeRole"),
    ('billing', 'MANAGER'::"EmployeeRole"),
    ('settings', 'OWNER'::"EmployeeRole"),
    ('online-reservations', 'OWNER'::"EmployeeRole"),
    ('online-reservations', 'MANAGER'::"EmployeeRole"),
    ('online-reservations', 'AGENT'::"EmployeeRole"),
    ('offers', 'OWNER'::"EmployeeRole"),
    ('offers', 'MANAGER'::"EmployeeRole"),
    ('team', 'OWNER'::"EmployeeRole"),
    ('team', 'MANAGER'::"EmployeeRole"),
    ('team', 'AGENT'::"EmployeeRole"),
    ('contracts', 'OWNER'::"EmployeeRole"),
    ('contracts', 'MANAGER'::"EmployeeRole"),
    ('contracts', 'AGENT'::"EmployeeRole"),
    ('notifications', 'OWNER'::"EmployeeRole"),
    ('notifications', 'MANAGER'::"EmployeeRole"),
    ('notifications', 'AGENT'::"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",
  "createdAt",
  "updatedAt"
)
SELECT
  'menu_role_' || lower(crv."role"::text) || '_' || crv."systemKey",
  mi."id",
  crv."role",
  CURRENT_TIMESTAMP,
  CURRENT_TIMESTAMP
FROM canonical_role_visibilities crv
JOIN "menu_items" mi ON mi."systemKey" = crv."systemKey"
ON CONFLICT ("menuItemId", "role") DO NOTHING;
