-- Add journey_list_backdated permission (run once on dev/live via phpMyAdmin).
-- Mirrors menus id 156 (booking_discount) pattern under ticket_booking (parent id 1).

INSERT INTO `menus` (`menu_title`, `page_url`, `module_name`, `parent_menu_id`, `have_chield`, `created_by`, `created_at`, `updated_at`)
SELECT 'journey_list_backdated', 'ticket/booking/journeylist', 'Ticket', 1, NULL, 1, NOW(), NOW()
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM `menus` WHERE `menu_title` = 'journey_list_backdated'
);

SET @menu_id := (
    SELECT `id` FROM `menus` WHERE `menu_title` = 'journey_list_backdated' LIMIT 1
);

INSERT INTO `permissions` (`role_id`, `user_id`, `menu_id`, `menu_title`, `create`, `read`, `edit`, `delete`, `created_at`, `updated_at`)
SELECT
    r.`id`,
    1,
    @menu_id,
    'journey_list_backdated',
    0,
    IF(r.`id` IN (1, 7), 1, 0),
    0,
    0,
    NOW(),
    NOW()
FROM `roles` r
WHERE NOT EXISTS (
    SELECT 1
    FROM `permissions` p
    WHERE p.`role_id` = r.`id`
      AND p.`menu_title` = 'journey_list_backdated'
);
