
ALTER TABLE was_openai_niche_result
 ADD COLUMN `niche_type` ENUM('niche', 'brand') NOT NULL DEFAULT 'niche' AFTER `niche_input`,
 DROP KEY `idx_niche_input`,
 ADD KEY `idx_niche_input_type` (`niche_input`, `niche_type`);

CREATE TABLE `was_openai_niche_user_favorite` (
  `openai_niche_user_favorite_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `niche_tag` varchar(100) NOT NULL,
  `niche_type` ENUM('niche', 'brand') NOT NULL DEFAULT 'niche',
  `user_id` int(11) unsigned NOT NULL,
  `openai_niche_result_id` int(11) unsigned NOT NULL,
  `insert_timestamp` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `update_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`openai_niche_user_favorite_id`),
  KEY `idx_user_id_niche_tag_niche_type` (`user_id`, `niche_tag`, `niche_type`),
  KEY `idx_niche_result_id` (`openai_niche_result_id`)
) ENGINE=InnoDB;

CREATE TRIGGER `was_openai_niche_user_favorite_insert_timestamp` BEFORE INSERT ON `was_openai_niche_user_favorite` FOR EACH ROW SET NEW.insert_timestamp = NOW();

ALTER TABLE `was_openai_niche_result`
 ADD `parent_openai_niche_result_id` INT(11) UNSIGNED NOT NULL AFTER `niche_result`,
 ADD KEY `idx_parent_openai_niche_result_id` (`parent_openai_niche_result_id`);
