-- Sipariş yasal onay kaydı, ilişkili ürünler, sipariş/satır varyant bilgisi, sepet satırı varyant anahtarı.
-- Idempotent (information_schema kontrolleri).

-- ecommerce_orders.checkout_legal_json — ön bilgi / mesafeli / iade / gizlilik onay zaman damgası
SET @c := (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'ecommerce_orders' AND COLUMN_NAME = 'checkout_legal_json'
);
SET @s := IF(@c = 0,
  'ALTER TABLE ecommerce_orders ADD COLUMN checkout_legal_json TEXT NULL AFTER notes',
  'SELECT 1');
PREPARE stmt FROM @s; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- products.sku — temel stok kodu (varyantsız veya ana kayıt)
SET @c := (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'products' AND COLUMN_NAME = 'sku'
);
SET @s := IF(@c = 0,
  'ALTER TABLE products ADD COLUMN sku VARCHAR(128) NULL DEFAULT NULL AFTER slug',
  'SELECT 1');
PREPARE stmt FROM @s; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- products.related_product_ids — JSON dizi [1,2,3]
SET @c := (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'products' AND COLUMN_NAME = 'related_product_ids'
);
SET @s := IF(@c = 0,
  'ALTER TABLE products ADD COLUMN related_product_ids JSON NULL AFTER stock_quantity',
  'SELECT 1');
PREPARE stmt FROM @s; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- ecommerce_order_items — satır varyantı
SET @c := (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'ecommerce_order_items' AND COLUMN_NAME = 'variant_id'
);
SET @s := IF(@c = 0,
  'ALTER TABLE ecommerce_order_items ADD COLUMN variant_id BIGINT UNSIGNED NULL DEFAULT NULL AFTER product_id',
  'SELECT 1');
PREPARE stmt FROM @s; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @c := (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'ecommerce_order_items' AND COLUMN_NAME = 'variant_sku'
);
SET @s := IF(@c = 0,
  'ALTER TABLE ecommerce_order_items ADD COLUMN variant_sku VARCHAR(128) NULL DEFAULT NULL AFTER title',
  'SELECT 1');
PREPARE stmt FROM @s; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @c := (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'ecommerce_order_items' AND COLUMN_NAME = 'variant_option_label'
);
SET @s := IF(@c = 0,
  'ALTER TABLE ecommerce_order_items ADD COLUMN variant_option_label VARCHAR(255) NULL DEFAULT NULL AFTER variant_sku',
  'SELECT 1');
PREPARE stmt FROM @s; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @fk := (
  SELECT COUNT(*) FROM information_schema.TABLE_CONSTRAINTS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'ecommerce_order_items' AND CONSTRAINT_NAME = 'fk_ecommerce_order_items_variant'
);
SET @s := IF(@fk = 0,
  'ALTER TABLE ecommerce_order_items ADD CONSTRAINT fk_ecommerce_order_items_variant FOREIGN KEY (variant_id) REFERENCES product_variants(id) ON DELETE SET NULL',
  'SELECT 1');
PREPARE stmt FROM @s; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- shop_cart_lines.variant_id (0 = varyant yok)
SET @c := (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'shop_cart_lines' AND COLUMN_NAME = 'variant_id'
);
SET @s := IF(@c = 0,
  'ALTER TABLE shop_cart_lines ADD COLUMN variant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER product_id',
  'SELECT 1');
PREPARE stmt FROM @s; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @ix := (
  SELECT COUNT(*) FROM information_schema.STATISTICS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'shop_cart_lines' AND INDEX_NAME = 'uq_shop_cart_customer_product'
);
SET @s := IF(@ix > 0,
  'ALTER TABLE shop_cart_lines DROP INDEX uq_shop_cart_customer_product',
  'SELECT 1');
PREPARE stmt FROM @s; EXECUTE stmt; DEALLOCATE PREPARE stmt;

SET @ix := (
  SELECT COUNT(*) FROM information_schema.STATISTICS
  WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'shop_cart_lines' AND INDEX_NAME = 'uq_shop_cart_customer_product_variant'
);
SET @s := IF(@ix = 0,
  'ALTER TABLE shop_cart_lines ADD UNIQUE KEY uq_shop_cart_customer_product_variant (customer_id, product_id, variant_id)',
  'SELECT 1');
PREPARE stmt FROM @s; EXECUTE stmt; DEALLOCATE PREPARE stmt;
