-- Zrzut struktury tabela host700513_adspro.clients
CREATE TABLE IF NOT EXISTS `clients` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL DEFAULT '0',
  `google_ads_customer_id` varchar(20) DEFAULT NULL,
  `google_merchant_account_id` varchar(32) DEFAULT NULL,
  `google_ads_start_date` date DEFAULT NULL,
  `active` int(11) DEFAULT 0,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=48 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `campaigns` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `client_id` int(11) NOT NULL DEFAULT 0,
  `campaign_id` bigint(20) NOT NULL DEFAULT 0,
  `campaign_name` varchar(255) NOT NULL DEFAULT '0',
  `advertising_channel_type` varchar(40) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `client_id` (`client_id`),
  CONSTRAINT `FK__clients` FOREIGN KEY (`client_id`) REFERENCES `clients` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=56 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Zrzut struktury tabela host700513_adspro.campaigns_history
CREATE TABLE IF NOT EXISTS `campaigns_history` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `campaign_id` int(11) NOT NULL DEFAULT 0,
  `roas_30_days` decimal(20,6) NOT NULL DEFAULT 0.000000,
  `roas_all_time` decimal(20,6) NOT NULL DEFAULT 0.000000,
  `budget` decimal(20,6) NOT NULL DEFAULT 0.000000,
  `money_spent` decimal(20,6) NOT NULL DEFAULT 0.000000,
  `conversion_value` decimal(20,6) NOT NULL DEFAULT 0.000000,
  `bidding_strategy` text DEFAULT NULL,
  `date_add` date NOT NULL DEFAULT '0000-00-00',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `offer_id` (`campaign_id`) USING BTREE,
  CONSTRAINT `FK_campaigns_history_campaigns` FOREIGN KEY (`campaign_id`) REFERENCES `campaigns` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=381 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC;

CREATE TABLE IF NOT EXISTS `campaign_ad_groups` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `campaign_id` int(11) NOT NULL,
  `ad_group_id` bigint(20) NOT NULL,
  `ad_group_name` varchar(255) NOT NULL DEFAULT '',
  `impressions_30` int(11) NOT NULL DEFAULT 0,
  `clicks_30` int(11) NOT NULL DEFAULT 0,
  `cost_30` decimal(20,6) NOT NULL DEFAULT 0.000000,
  `conversions_30` decimal(20,6) NOT NULL DEFAULT 0.000000,
  `conversion_value_30` decimal(20,6) NOT NULL DEFAULT 0.000000,
  `roas_30` decimal(20,6) NOT NULL DEFAULT 0.000000,
  `impressions_all_time` int(11) NOT NULL DEFAULT 0,
  `clicks_all_time` int(11) NOT NULL DEFAULT 0,
  `cost_all_time` decimal(20,6) NOT NULL DEFAULT 0.000000,
  `conversions_all_time` decimal(20,6) NOT NULL DEFAULT 0.000000,
  `conversion_value_all_time` decimal(20,6) NOT NULL DEFAULT 0.000000,
  `roas_all_time` decimal(20,6) NOT NULL DEFAULT 0.000000,
  `date_sync` date DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_campaign_ad_groups_campaign_ad_group` (`campaign_id`,`ad_group_id`),
  KEY `idx_campaign_ad_groups_campaign_id` (`campaign_id`),
  CONSTRAINT `FK_campaign_ad_groups_campaigns` FOREIGN KEY (`campaign_id`) REFERENCES `campaigns` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=125 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `campaign_alerts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `client_id` int(11) NOT NULL,
  `campaign_id` int(11) DEFAULT NULL,
  `campaign_external_id` bigint(20) DEFAULT NULL,
  `ad_group_id` int(11) DEFAULT NULL,
  `ad_group_external_id` bigint(20) DEFAULT NULL,
  `product_id` int(11) DEFAULT NULL,
  `alert_type` varchar(120) NOT NULL,
  `message` text NOT NULL,
  `meta_json` text DEFAULT NULL,
  `date_detected` date NOT NULL,
  `date_add` datetime NOT NULL DEFAULT current_timestamp(),
  `unseen` tinyint(1) NOT NULL DEFAULT 1,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_alert_daily` (`client_id`,`campaign_external_id`,`ad_group_external_id`,`alert_type`,`date_detected`),
  KEY `idx_alert_date` (`date_detected`),
  KEY `idx_alert_client` (`client_id`),
  KEY `idx_alert_campaign` (`campaign_id`),
  KEY `idx_alert_ad_group` (`ad_group_id`),
  KEY `idx_alert_unseen` (`unseen`),
  KEY `idx_alert_product` (`product_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

CREATE TABLE IF NOT EXISTS `products` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `client_id` int(11) NOT NULL DEFAULT 0,
  `offer_id` varchar(50) NOT NULL DEFAULT '0',
  `name` varchar(255) NOT NULL DEFAULT '0',
  `min_roas` int(11) DEFAULT NULL,
  `custom_label_4` varchar(255) DEFAULT NULL,
  `unit_pricing_measure` varchar(64) DEFAULT NULL,
  `unit_pricing_base_measure` varchar(64) DEFAULT NULL,
  `unit_pricing_changed_at` datetime DEFAULT NULL,
  `custom_label_3` varchar(255) DEFAULT NULL,
  `title` varchar(255) DEFAULT NULL,
  `description` text DEFAULT NULL,
  `google_product_category` text DEFAULT NULL,
  `product_url` varchar(500) DEFAULT NULL,
  `merchant_url_not_found` tinyint(1) NOT NULL DEFAULT 0,
  `merchant_url_last_check` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `FK_offers_clients` (`client_id`),
  CONSTRAINT `FK_offers_clients` FOREIGN KEY (`client_id`) REFERENCES `clients` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=8482 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `products_history` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `product_id` int(11) NOT NULL DEFAULT 0,
  `campaign_id` int(11) NOT NULL DEFAULT 0,
  `ad_group_id` int(11) NOT NULL DEFAULT 0,
  `impressions` int(11) NOT NULL DEFAULT 0,
  `clicks` int(11) NOT NULL DEFAULT 0,
  `ctr` decimal(20,6) NOT NULL DEFAULT 0.000000,
  `cost` decimal(20,6) NOT NULL DEFAULT 0.000000,
  `conversions` decimal(20,6) NOT NULL DEFAULT 0.000000,
  `conversions_value` decimal(20,6) NOT NULL DEFAULT 0.000000,
  `date_add` date NOT NULL DEFAULT '0000-00-00',
  `updated` int(11) NOT NULL DEFAULT 0,
  `deleted` int(11) DEFAULT 0,
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE KEY `uk_products_history_scope_day` (`product_id`,`campaign_id`,`ad_group_id`,`date_add`),
  KEY `product_id` (`product_id`) USING BTREE,
  KEY `idx_products_history_campaign_id` (`campaign_id`),
  KEY `idx_products_history_ad_group_id` (`ad_group_id`),
  CONSTRAINT `FK_products_history_products` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=37033 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC;

CREATE TABLE IF NOT EXISTS `products_history_30` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `product_id` int(11) NOT NULL,
  `campaign_id` int(11) NOT NULL DEFAULT 0,
  `ad_group_id` int(11) NOT NULL DEFAULT 0,
  `impressions` int(11) NOT NULL,
  `clicks` int(11) NOT NULL,
  `ctr` decimal(20,6) NOT NULL DEFAULT 0.000000,
  `cost` decimal(20,6) NOT NULL,
  `conversions` decimal(20,6) NOT NULL DEFAULT 0.000000,
  `conversions_value` decimal(20,6) NOT NULL,
  `roas` decimal(20,6) NOT NULL,
  `roas_all_time` decimal(20,6) NOT NULL,
  `date_add` date NOT NULL DEFAULT '0000-00-00',
  `deleted` int(11) DEFAULT 0,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_products_history_30_scope_day` (`product_id`,`campaign_id`,`ad_group_id`,`date_add`),
  KEY `product_id` (`product_id`) USING BTREE,
  KEY `idx_products_history_30_campaign_id` (`campaign_id`),
  KEY `idx_products_history_30_ad_group_id` (`ad_group_id`),
  CONSTRAINT `FK_products_history_30_products` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `products_aggregate` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `product_id` int(11) NOT NULL,
  `campaign_id` int(11) NOT NULL DEFAULT 0,
  `ad_group_id` int(11) NOT NULL DEFAULT 0,
  `impressions_30` int(11) NOT NULL DEFAULT 0,
  `clicks_30` int(11) NOT NULL DEFAULT 0,
  `ctr_30` decimal(20,6) NOT NULL DEFAULT 0.000000,
  `cost_30` decimal(20,6) NOT NULL DEFAULT 0.000000,
  `conversions_30` decimal(20,6) NOT NULL DEFAULT 0.000000,
  `conversion_value_30` decimal(20,6) NOT NULL DEFAULT 0.000000,
  `roas_30` decimal(20,6) NOT NULL DEFAULT 0.000000,
  `impressions_all_time` int(11) NOT NULL DEFAULT 0,
  `clicks_all_time` int(11) NOT NULL DEFAULT 0,
  `ctr_all_time` decimal(20,6) NOT NULL DEFAULT 0.000000,
  `cost_all_time` decimal(20,6) NOT NULL DEFAULT 0.000000,
  `conversions_all_time` decimal(20,6) NOT NULL DEFAULT 0.000000,
  `conversion_value_all_time` decimal(20,6) NOT NULL DEFAULT 0.000000,
  `roas_all_time` decimal(20,6) NOT NULL DEFAULT 0.000000,
  `date_sync` date NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_products_aggregate_scope` (`product_id`,`campaign_id`,`ad_group_id`),
  KEY `idx_products_aggregate_campaign_id` (`campaign_id`),
  KEY `idx_products_aggregate_ad_group_id` (`ad_group_id`),
  KEY `idx_products_aggregate_date_sync` (`date_sync`),
  CONSTRAINT `FK_products_aggregate_products` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
