-- HOTFIX: ensure vouchers table exists (some environments had 006 marked applied while vouchers table was not created)

CREATE TABLE IF NOT EXISTS vouchers (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (id),
  tenant_id BIGINT UNSIGNED NOT NULL,
  code VARCHAR(64) NOT NULL,
  title VARCHAR(191) NOT NULL,
  type ENUM('percent','fixed') NOT NULL DEFAULT 'percent',
  value DECIMAL(18,2) NOT NULL DEFAULT 0,
  max_discount DECIMAL(18,2) NULL,
  min_order_amount DECIMAL(18,2) NULL,
  start_at DATETIME NULL,
  end_at DATETIME NULL,
  usage_limit_total INT NULL,
  usage_limit_per_customer INT NULL,
  is_active TINYINT(1) NOT NULL DEFAULT 1,
  created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  UNIQUE KEY uq_voucher_code_tenant (tenant_id, code),
  INDEX idx_vouchers_tenant_active (tenant_id, is_active),
  INDEX idx_vouchers_period (start_at, end_at)
);
