
SET @db = DATABASE();

SET @sql = IF (
    EXISTS (
        SELECT 1
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_SCHEMA = @db
          AND TABLE_NAME = 'product_packs'
          AND COLUMN_NAME = 'global_discount_percent'
    ),
    'SELECT 1',
    'ALTER TABLE product_packs ADD COLUMN global_discount_percent DECIMAL(8,4) NOT NULL DEFAULT 0.0000 AFTER description'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF (
    EXISTS (
        SELECT 1
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_SCHEMA = @db
          AND TABLE_NAME = 'product_pack_items'
          AND COLUMN_NAME = 'criteria_json'
    ),
    'SELECT 1',
    'ALTER TABLE product_pack_items ADD COLUMN criteria_json JSON NULL AFTER source_product_id'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF (
    EXISTS (
        SELECT 1
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_SCHEMA = @db
          AND TABLE_NAME = 'product_pack_items'
          AND COLUMN_NAME = 'unit_sale_ht_override'
    ),
    'SELECT 1',
    'ALTER TABLE product_pack_items ADD COLUMN unit_sale_ht_override DECIMAL(12,4) NULL AFTER quantity'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF (
    EXISTS (
        SELECT 1
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_SCHEMA = @db
          AND TABLE_NAME = 'shop_products'
          AND COLUMN_NAME = 'promotion_enabled'
    ),
    'SELECT 1',
    'ALTER TABLE shop_products ADD COLUMN promotion_enabled TINYINT NOT NULL DEFAULT 0 AFTER is_visible'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @sql = IF (
    EXISTS (
        SELECT 1
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_SCHEMA = @db
          AND TABLE_NAME = 'shop_products'
          AND COLUMN_NAME = 'promotion_percent'
    ),
    'SELECT 1',
    'ALTER TABLE shop_products ADD COLUMN promotion_percent DECIMAL(8,4) NOT NULL DEFAULT 0.0000 AFTER promotion_enabled'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
