DELETE FROM `engine4_core_tasks` WHERE `engine4_core_tasks`.`plugin` = "Core_Plugin_Task_Prefetch";

UPDATE `engine4_core_tasks` SET `timeout` = '10' WHERE `engine4_core_tasks`.`plugin` = "Core_Plugin_Task_Jobs";
UPDATE `engine4_core_tasks` SET `timeout` = '30' WHERE `engine4_core_tasks`.`plugin` = "Core_Plugin_Task_Mail";
UPDATE `engine4_core_tasks` SET `timeout` = '86400' WHERE `engine4_core_tasks`.`plugin` = "Core_Plugin_Task_LogRotation";

-- Help & Support menu for Mobile
INSERT IGNORE INTO `engine4_core_menus` (`name`, `type`, `title`, `order`) VALUES
('core_helpsupport_mobile', 'standard', 'Help & Support for Mobile', 999);

INSERT IGNORE INTO `engine4_core_menuitems` (`name`, `module`, `label`, `plugin`, `params`, `menu`, `submenu`, `order`) VALUES
('core_helpsupport_mobile_settings', 'user', 'Settings', 'User_Plugin_Menus', '{"icon":"fi fi-rr-settings"}', 'core_helpsupport_mobile', '', 1),
('core_helpsupport_mobile_privacy', 'core', 'Privacy', '', '{"route":"default","module":"core","controller":"help","action":"privacy"}', 'core_helpsupport_mobile', '', 2),
('core_helpsupport_mobile_terms', 'core', 'Terms of Service', '', '{"route":"default","module":"core","controller":"help","action":"terms"}', 'core_helpsupport_mobile', '', 3),
('core_helpsupport_mobile_supportinbox', 'core', 'Support Inbox', 'User_Plugin_Menus', '{"route":"user_support","module":"user","controller":"support","action":"index", "icon":"fi fi-rr-user-headset"}', 'core_helpsupport_mobile', '', 4),
('core_helpsupport_mobile_contact', 'core', 'Contact', '', '{"route":"default","module":"core","controller":"help","action":"contact"}', 'core_helpsupport_mobile', '', 5),
('core_helpsupport_mobile_invite', 'invite', 'Invite', 'Invite_Plugin_Menus::canInvite', '{"route":"default","module":"invite","icon":"fa fa-envelope"}', 'core_helpsupport_mobile', '', 6);


-- =============================================================
-- Save to Collection: Tables
-- =============================================================

CREATE TABLE IF NOT EXISTS `engine4_core_saveditems` (
  `saveditem_id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id` INT(11) UNSIGNED NOT NULL,
  `content_type` VARCHAR(64) NOT NULL,
  `content_id` INT(11) UNSIGNED NOT NULL,
  `created_at` DATETIME NOT NULL,
  PRIMARY KEY (`saveditem_id`),
  UNIQUE KEY `uk_user_content` (`user_id`, `content_type`, `content_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_content` (`content_type`, `content_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `engine4_core_collections` (
  `collection_id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id` INT(11) UNSIGNED NOT NULL,
  `name` VARCHAR(50) NOT NULL,
  `usage_count` INT(11) UNSIGNED NOT NULL DEFAULT 0,
  `created_at` DATETIME NOT NULL,
  `updated_at` DATETIME NOT NULL,
  PRIMARY KEY (`collection_id`),
  UNIQUE KEY `uk_user_name` (`user_id`, `name`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_user_updated` (`user_id`, `updated_at` DESC)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `engine4_core_collectionitems` (
  `collectionitem_id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `collection_id` INT(11) UNSIGNED NOT NULL,
  `saveditem_id` INT(11) UNSIGNED NOT NULL,
  `user_id` INT(11) UNSIGNED NOT NULL,
  `created_at` DATETIME NOT NULL,
  PRIMARY KEY (`collectionitem_id`),
  UNIQUE KEY `uk_collection_saveditem` (`collection_id`, `saveditem_id`),
  KEY `idx_saveditem_id` (`saveditem_id`),
  KEY `idx_collection_id` (`collection_id`),
  KEY `idx_user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- =============================================================
-- Save to Collection: Settings (savecollection. prefix)
-- =============================================================

INSERT IGNORE INTO `engine4_core_settings` (`name`, `value`) VALUES
('savecollection.enabled', '1'),
('savecollection.pagination.default', '20'),
('savecollection.pagination.min', '10'),
('savecollection.pagination.max', '50');

-- =============================================================
-- Save to Collection: Menu Item Registration
-- =============================================================

INSERT IGNORE INTO `engine4_core_menuitems` (`name`, `module`, `label`, `plugin`, `params`, `menu`, `submenu`, `enabled`, `custom`, `order`) VALUES
('core_minimenuquick_saveditems', 'core', 'Saved Items', 'Core_Plugin_Menus::canViewSaveditems', '{"route":"default","module":"core","controller":"savecollection","action":"index","icon":"fi fi-rr-bookmark"}', 'core_minimenuquick', '', 1, 0, 999);



DELETE from `engine4_activity_filterlists` WHERE `filtertype` = 'saved_feeds';

INSERT INTO engine4_core_saveditems (
    user_id,
    content_type,
    content_id,
    created_at
)
SELECT
    sf.user_id,
    'activity_action',
    sf.action_id,
    NOW()
FROM engine4_activity_savefeeds sf
LEFT JOIN engine4_core_saveditems si
    ON si.user_id = sf.user_id
    AND si.content_type = 'activity_action'
    AND si.content_id = sf.action_id
WHERE si.saveditem_id IS NULL;
INSERT IGNORE INTO `engine4_core_menuitems` (`name`, `module`, `label`, `plugin`, `params`, `menu`, `submenu`, `order`) VALUES ('core_minimenu_wallet', 'core', 'Wallet', 'User_Plugin_Menus', '{"route":"default","module":"payment","controller":"settings","action":"wallet", "icon":"fi fi-rr-wallet"}', 'core_minimenuquick', '', 4);
