-- Migracja: tabela historii dziennych fraz wyszukiwanych
-- Opis: przejscie z modelu snapshot (30d + all-time) na dane dzienne z agregacja w bazie

CREATE TABLE IF NOT EXISTS `campaign_search_terms_history` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `campaign_id` INT(11) NOT NULL,
  `ad_group_id` INT(11) NOT NULL,
  `search_term` VARCHAR(255) NOT NULL,
  `impressions` INT(11) NOT NULL DEFAULT 0,
  `clicks` INT(11) NOT NULL DEFAULT 0,
  `cost` DECIMAL(20,6) NOT NULL DEFAULT 0.000000,
  `conversions` DECIMAL(20,6) NOT NULL DEFAULT 0.000000,
  `conversion_value` DECIMAL(20,6) NOT NULL DEFAULT 0.000000,
  `date_add` DATE NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_search_terms_history` (`campaign_id`, `ad_group_id`, `search_term`, `date_add`),
  KEY `idx_search_terms_history_campaign_id` (`campaign_id`),
  KEY `idx_search_terms_history_ad_group_id` (`ad_group_id`),
  KEY `idx_search_terms_history_date_add` (`date_add`),
  CONSTRAINT `FK_search_terms_history_campaigns`
    FOREIGN KEY (`campaign_id`) REFERENCES `campaigns` (`id`)
    ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `FK_search_terms_history_ad_groups`
    FOREIGN KEY (`ad_group_id`) REFERENCES `campaign_ad_groups` (`id`)
    ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
