
CREATE TABLE `was_openai_niche_result` (
  `openai_niche_result_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `niche_input` varchar(100) NOT NULL,
  `niche_prompt` TEXT NOT NULL,
  `niche_result` TEXT 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_result_id`),
  KEY `idx_niche_input` (`niche_input`)
) ENGINE=InnoDB;

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


CREATE TABLE `was_openai_niche_user_history` (
  `openai_niche_user_history_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `openai_niche_result_id` int(11) unsigned NOT NULL,
  `user_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_history_id`),
  KEY `idx_openai_niche_result_id` (`openai_niche_result_id`),
  KEY `idx_user_id` (`user_id`)
) ENGINE=InnoDB;

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


CREATE TABLE `was_openai_niche_user_search_count` (
  `openai_niche_user_search_count_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `user_id` int(11) unsigned NOT NULL,
  `search_count` tinyint(4) unsigned NOT NULL,
  `reset_timestamp` timestamp NULL DEFAULT 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_search_count_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_reset_timestamp` (`reset_timestamp`)
) ENGINE=InnoDB;

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