-- ClickBack fresh database schema
-- MySQL 5.7+ / MariaDB 10.5+
-- Charset: utf8mb4

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

CREATE TABLE IF NOT EXISTS hy_shops (
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  shop_domain VARCHAR(255) NOT NULL,
  shop_name VARCHAR(255) DEFAULT NULL,
  email VARCHAR(255) DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uq_hy_shops_domain (shop_domain)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS hy_shop_installs (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  shop_id INT UNSIGNED NOT NULL,
  app_name VARCHAR(80) NOT NULL,
  access_token VARCHAR(512) NOT NULL,
  scope VARCHAR(512) DEFAULT NULL,
  plan_name VARCHAR(64) DEFAULT NULL,
  billing_status ENUM('none','trialing','pending','active','paused','cancelled','expired') NOT NULL DEFAULT 'none',
  charge_id BIGINT UNSIGNED DEFAULT NULL,
  installed_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  uninstalled_at DATETIME DEFAULT NULL,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uq_hy_shop_installs_shop_app (shop_id, app_name),
  KEY idx_hy_shop_installs_app_name (app_name),
  KEY idx_hy_shop_installs_billing_status (billing_status),
  CONSTRAINT fk_hy_shop_installs_shop
    FOREIGN KEY (shop_id) REFERENCES hy_shops (id)
    ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS hy_services_logs (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  shop_id INT UNSIGNED DEFAULT NULL,
  app_name VARCHAR(80) DEFAULT NULL,
  service_name VARCHAR(100) DEFAULT NULL,
  status ENUM('success','error','info') NOT NULL DEFAULT 'success',
  confirmation TINYINT(1) NOT NULL DEFAULT 0,
  action VARCHAR(255) NOT NULL,
  json_data JSON NOT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_hy_services_logs_shop_id (shop_id),
  KEY idx_hy_services_logs_app_name (app_name),
  KEY idx_hy_services_logs_created_at (created_at),
  CONSTRAINT fk_hy_services_logs_shop
    FOREIGN KEY (shop_id) REFERENCES hy_shops (id)
    ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS dr_shop_settings (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  shop_id INT UNSIGNED NOT NULL,
  tracking_enabled TINYINT(1) NOT NULL DEFAULT 1,
  session_ttl_hours SMALLINT UNSIGNED NOT NULL DEFAULT 720,
  cookie_name VARCHAR(80) NOT NULL DEFAULT 'dr_visitor',
  settings_json JSON NOT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uq_dr_shop_settings_shop (shop_id),
  CONSTRAINT fk_dr_shop_settings_shop
    FOREIGN KEY (shop_id) REFERENCES hy_shops (id)
    ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS dr_plan_subscriptions (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  shop_id INT UNSIGNED NOT NULL,
  plan_key VARCHAR(20) NOT NULL DEFAULT 'free',
  plan_status ENUM('free','trialing','active','past_due','cancelled') NOT NULL DEFAULT 'free',
  billing_status ENUM('none','trialing','pending','active','paused','cancelled','expired') NOT NULL DEFAULT 'none',
  shopify_charge_id VARCHAR(64) DEFAULT NULL,
  trial_started_at DATETIME DEFAULT NULL,
  trial_ends_at DATETIME DEFAULT NULL,
  current_period_started_at DATETIME DEFAULT NULL,
  current_period_ends_at DATETIME DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uq_dr_plan_subscriptions_shop (shop_id),
  KEY idx_dr_plan_subscriptions_plan (plan_key, plan_status),
  CONSTRAINT fk_dr_plan_subscriptions_shop
    FOREIGN KEY (shop_id) REFERENCES hy_shops (id)
    ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS dr_plan_usage_monthly (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  shop_id INT UNSIGNED NOT NULL,
  usage_month DATE NOT NULL,
  trigger_count INT UNSIGNED NOT NULL DEFAULT 0,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uq_dr_plan_usage_monthly_shop_month (shop_id, usage_month),
  KEY idx_dr_plan_usage_monthly_month (usage_month),
  CONSTRAINT fk_dr_plan_usage_monthly_shop
    FOREIGN KEY (shop_id) REFERENCES hy_shops (id)
    ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS dr_discount_rules (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  shop_id INT UNSIGNED NOT NULL,
  name VARCHAR(150) NOT NULL,
  description VARCHAR(500) DEFAULT NULL,
  rule_type VARCHAR(20) NOT NULL DEFAULT 'product',
  application_mode VARCHAR(20) NOT NULL DEFAULT 'automatic',
  visit_threshold TINYINT UNSIGNED NOT NULL DEFAULT 3,
  discount_type ENUM('percent','fixed','free_shipping') NOT NULL DEFAULT 'percent',
  discount_value DECIMAL(10,2) NOT NULL DEFAULT 10.00,
  discount_config JSON NULL,
  timer_minutes SMALLINT UNSIGNED NOT NULL DEFAULT 30,
  timer_enabled TINYINT(1) UNSIGNED NOT NULL DEFAULT 1,
  applies_to ENUM('all_products','specific_products','specific_collections') NOT NULL DEFAULT 'all_products',
  widget_style ENUM('banner','badge','popup','sticky','inline') NOT NULL DEFAULT 'banner',
  widget_bg_transparent TINYINT(1) UNSIGNED NOT NULL DEFAULT 0,
  widget_display ENUM('product','cart','all','product_cart_checkout') NOT NULL DEFAULT 'all',
  widget_text VARCHAR(255) NOT NULL DEFAULT 'Votre remise expire dans [TIMER] !',
  widget_bg_color VARCHAR(7) NOT NULL DEFAULT '#dc2626',
  widget_text_color VARCHAR(7) NOT NULL DEFAULT '#ffffff',
  widget_font_size TINYINT UNSIGNED NOT NULL DEFAULT 14,
  widget_border_radius TINYINT UNSIGNED NOT NULL DEFAULT 8,
  widget_padding_v TINYINT UNSIGNED NOT NULL DEFAULT 10,
  widget_padding_h TINYINT UNSIGNED NOT NULL DEFAULT 16,
  widget_border_type ENUM('none','solid','dashed','dotted') NOT NULL DEFAULT 'none',
  widget_border_color VARCHAR(7) NOT NULL DEFAULT '#e5e7eb',
  widget_border_width TINYINT UNSIGNED NOT NULL DEFAULT 1,
  widget_code_position ENUM('beside','below') NOT NULL DEFAULT 'beside',
  widget_btn_bg_color VARCHAR(7) NOT NULL DEFAULT '#ffffff',
  widget_btn_text_color VARCHAR(7) NOT NULL DEFAULT '#374151',
  widget_btn_border_color VARCHAR(7) NOT NULL DEFAULT '#d1d5db',
  widget_btn_font_size TINYINT UNSIGNED NOT NULL DEFAULT 12,
  widget_btn_border_radius TINYINT UNSIGNED NOT NULL DEFAULT 4,
  status ENUM('draft','active','disabled') NOT NULL DEFAULT 'draft',
  priority SMALLINT UNSIGNED NOT NULL DEFAULT 100,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_dr_discount_rules_shop_status (shop_id, status),
  KEY idx_dr_discount_rules_shop_priority (shop_id, priority),
  KEY idx_dr_discount_rules_created_at (created_at),
  CONSTRAINT fk_dr_discount_rules_shop
    FOREIGN KEY (shop_id) REFERENCES hy_shops (id)
    ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS dr_discount_rule_targets (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  rule_id BIGINT UNSIGNED NOT NULL,
  shop_id INT UNSIGNED NOT NULL,
  target_type ENUM('product','collection') NOT NULL,
  shopify_id VARCHAR(30) NOT NULL,
  handle VARCHAR(255) NOT NULL DEFAULT '',
  title VARCHAR(255) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uq_dr_discount_rule_targets_rule_target (rule_id, target_type, shopify_id),
  KEY idx_dr_discount_rule_targets_shop_type (shop_id, target_type),
  KEY idx_dr_discount_rule_targets_rule (rule_id),
  CONSTRAINT fk_dr_discount_rule_targets_rule
    FOREIGN KEY (rule_id) REFERENCES dr_discount_rules (id)
    ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT fk_dr_discount_rule_targets_shop
    FOREIGN KEY (shop_id) REFERENCES hy_shops (id)
    ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS dr_ab_tests (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  shop_id INT UNSIGNED NOT NULL,
  name VARCHAR(150) NOT NULL,
  rule_a_id BIGINT UNSIGNED DEFAULT NULL,
  rule_b_id BIGINT UNSIGNED DEFAULT NULL,
  split_percent TINYINT UNSIGNED NOT NULL DEFAULT 50,
  status ENUM('draft','scheduled','running','paused','completed') NOT NULL DEFAULT 'draft',
  winner_variant ENUM('a','b') DEFAULT NULL,
  started_at DATETIME DEFAULT NULL,
  ended_at DATETIME DEFAULT NULL,
  config JSON NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_dr_ab_tests_shop_status (shop_id, status),
  KEY idx_dr_ab_tests_rule_a (rule_a_id),
  KEY idx_dr_ab_tests_rule_b (rule_b_id),
  CONSTRAINT fk_dr_ab_tests_shop
    FOREIGN KEY (shop_id) REFERENCES hy_shops (id)
    ON DELETE CASCADE,
  CONSTRAINT fk_dr_ab_tests_rule_a
    FOREIGN KEY (rule_a_id) REFERENCES dr_discount_rules (id)
    ON DELETE SET NULL ON UPDATE CASCADE,
  CONSTRAINT fk_dr_ab_tests_rule_b
    FOREIGN KEY (rule_b_id) REFERENCES dr_discount_rules (id)
    ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS dr_visit_tracking (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  shop_id INT UNSIGNED NOT NULL,
  product_id VARCHAR(30) NOT NULL,
  visitor_token VARCHAR(128) NOT NULL,
  visit_count INT UNSIGNED NOT NULL DEFAULT 1,
  discount_triggered TINYINT(1) NOT NULL DEFAULT 0,
  discount_triggered_at DATETIME DEFAULT NULL,
  last_visited_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  expires_at DATETIME NOT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uq_dr_visit_tracking_shop_product_visitor (shop_id, product_id, visitor_token),
  KEY idx_dr_visit_tracking_expires (expires_at),
  CONSTRAINT fk_dr_visit_tracking_shop
    FOREIGN KEY (shop_id) REFERENCES hy_shops (id)
    ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS dr_storefront_discounts (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  shop_id INT UNSIGNED NOT NULL,
  product_id VARCHAR(30) NOT NULL,
  visitor_token VARCHAR(128) NOT NULL,
  discount_rule_id BIGINT UNSIGNED NOT NULL,
  ab_test_id BIGINT UNSIGNED DEFAULT NULL,
  variant ENUM('a','b') DEFAULT NULL,
  shopify_price_rule_id BIGINT UNSIGNED NOT NULL DEFAULT 0,
  shopify_discount_gid VARCHAR(120) DEFAULT NULL,
  shopify_discount_code_gid VARCHAR(120) DEFAULT NULL,
  discount_code VARCHAR(50) NOT NULL,
  discount_type ENUM('percent','fixed','free_shipping') NOT NULL,
  discount_value DECIMAL(10,2) NOT NULL,
  timer_minutes INT UNSIGNED NOT NULL,
  expires_at DATETIME DEFAULT NULL,
  creation_status ENUM('creating','created','failed') NOT NULL DEFAULT 'created',
  idempotency_key CHAR(64) DEFAULT NULL,
  creation_last_error VARCHAR(500) DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uq_dr_storefront_discounts_visitor_product (shop_id, product_id, visitor_token),
  KEY idx_dr_storefront_discounts_expires (expires_at),
  KEY idx_dr_storefront_discounts_ab (shop_id, ab_test_id, variant),
  KEY idx_dr_storefront_discount_status (shop_id, product_id, visitor_token, creation_status),
  KEY idx_dr_storefront_discount_graphql (shopify_discount_gid),
  KEY idx_dr_storefront_discount_code (shop_id, discount_code),
  KEY idx_dr_storefront_discount_rule (discount_rule_id),
  CONSTRAINT fk_dr_storefront_discounts_shop
    FOREIGN KEY (shop_id) REFERENCES hy_shops (id)
    ON DELETE CASCADE,
  CONSTRAINT fk_dr_storefront_discounts_rule
    FOREIGN KEY (discount_rule_id) REFERENCES dr_discount_rules (id)
    ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT fk_dr_storefront_discounts_ab_test
    FOREIGN KEY (ab_test_id) REFERENCES dr_ab_tests (id)
    ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS dr_conversion_events (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  shop_id INT UNSIGNED NOT NULL,
  rule_id BIGINT UNSIGNED NOT NULL,
  ab_test_id BIGINT UNSIGNED DEFAULT NULL,
  variant ENUM('a','b') DEFAULT NULL,
  visitor_token VARCHAR(128) NOT NULL,
  product_id VARCHAR(30) NOT NULL,
  event_type ENUM('discount_shown','discount_clicked','cart_add','purchase') NOT NULL,
  discount_code VARCHAR(50) DEFAULT NULL,
  order_id VARCHAR(30) DEFAULT NULL,
  order_amount_cents INT UNSIGNED DEFAULT NULL,
  occurred_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_dr_conversion_events_shop_test (shop_id, ab_test_id, variant),
  KEY idx_dr_conversion_events_rule (rule_id, event_type),
  KEY idx_dr_conversion_events_visitor (visitor_token, product_id),
  KEY idx_dr_conversion_events_order (shop_id, rule_id, event_type, order_id),
  KEY idx_dr_conversion_discount_click_once (shop_id, visitor_token, product_id, event_type, discount_code),
  CONSTRAINT fk_dr_conversion_events_shop
    FOREIGN KEY (shop_id) REFERENCES hy_shops (id)
    ON DELETE CASCADE,
  CONSTRAINT fk_dr_conversion_events_rule
    FOREIGN KEY (rule_id) REFERENCES dr_discount_rules (id)
    ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT fk_dr_conversion_events_ab_test
    FOREIGN KEY (ab_test_id) REFERENCES dr_ab_tests (id)
    ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS dr_analytics_daily (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  shop_id INT UNSIGNED NOT NULL,
  rule_id BIGINT UNSIGNED NOT NULL,
  ab_test_id BIGINT UNSIGNED DEFAULT NULL,
  variant ENUM('a','b') DEFAULT NULL,
  day DATE NOT NULL,
  discounts_shown INT UNSIGNED NOT NULL DEFAULT 0,
  discounts_clicked INT UNSIGNED NOT NULL DEFAULT 0,
  cart_adds INT UNSIGNED NOT NULL DEFAULT 0,
  purchases INT UNSIGNED NOT NULL DEFAULT 0,
  revenue_cents BIGINT UNSIGNED NOT NULL DEFAULT 0,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uq_dr_analytics_daily_day_rule_variant (rule_id, day, variant),
  KEY idx_dr_analytics_daily_shop_day (shop_id, day),
  KEY idx_dr_analytics_daily_ab_test_day (ab_test_id, day),
  CONSTRAINT fk_dr_analytics_daily_shop
    FOREIGN KEY (shop_id) REFERENCES hy_shops (id)
    ON DELETE CASCADE,
  CONSTRAINT fk_dr_analytics_daily_rule
    FOREIGN KEY (rule_id) REFERENCES dr_discount_rules (id)
    ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT fk_dr_analytics_daily_ab_test
    FOREIGN KEY (ab_test_id) REFERENCES dr_ab_tests (id)
    ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS dr_copy_events (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  shop_id INT UNSIGNED NOT NULL,
  visitor_token VARCHAR(128) NOT NULL,
  discount_code VARCHAR(50) NOT NULL,
  product_id VARCHAR(30) NOT NULL,
  clicked_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  KEY idx_dr_copy_events_shop (shop_id),
  KEY idx_dr_copy_events_code (discount_code),
  KEY idx_dr_copy_events_visitor (visitor_token),
  CONSTRAINT fk_dr_copy_events_shop
    FOREIGN KEY (shop_id) REFERENCES hy_shops (id)
    ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS dr_rate_limits (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  route_key VARCHAR(80) NOT NULL,
  identity_hash CHAR(64) NOT NULL,
  window_started_at DATETIME NOT NULL,
  request_count INT UNSIGNED NOT NULL DEFAULT 0,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (id),
  UNIQUE KEY uq_dr_rate_limits_route_identity (route_key, identity_hash),
  KEY idx_dr_rate_limits_updated (updated_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
