-- SQL equivalent of M260804120000AddScopedPlatformFeeThresholds::up().
-- Select the intended application database before running this file.

SET @zv_migration_sql = IF(
    (SELECT COUNT(*) FROM information_schema.columns
     WHERE table_schema = DATABASE()
       AND table_name = 'platform_fee_rules'
       AND column_name = 'order_min_minor_amount') = 0,
    'ALTER TABLE `platform_fee_rules` ADD COLUMN `order_min_minor_amount` INT NULL',
    'SELECT 1'
);
PREPARE zv_migration_stmt FROM @zv_migration_sql;
EXECUTE zv_migration_stmt;
DEALLOCATE PREPARE zv_migration_stmt;

SET @zv_migration_sql = IF(
    (SELECT COUNT(*) FROM information_schema.columns
     WHERE table_schema = DATABASE()
       AND table_name = 'platform_fee_rules'
       AND column_name = 'order_max_minor_amount') = 0,
    'ALTER TABLE `platform_fee_rules` ADD COLUMN `order_max_minor_amount` INT NULL',
    'SELECT 1'
);
PREPARE zv_migration_stmt FROM @zv_migration_sql;
EXECUTE zv_migration_stmt;
DEALLOCATE PREPARE zv_migration_stmt;

SET @zv_migration_sql = IF(
    (SELECT COUNT(*) FROM information_schema.statistics
     WHERE table_schema = DATABASE()
       AND table_name = 'platform_fee_rules'
       AND index_name = 'idx_fee_organiser_policy') = 0,
    'ALTER TABLE `platform_fee_rules` ADD KEY `idx_fee_organiser_policy` (`organiser_id`, `country`, `currency`, `is_active`)',
    'SELECT 1'
);
PREPARE zv_migration_stmt FROM @zv_migration_sql;
EXECUTE zv_migration_stmt;
DEALLOCATE PREPARE zv_migration_stmt;

SET @zv_migration_sql = IF(
    (SELECT COUNT(*) FROM information_schema.statistics
     WHERE table_schema = DATABASE()
       AND table_name = 'platform_fee_rules'
       AND index_name = 'idx_fee_order_range') = 0,
    'ALTER TABLE `platform_fee_rules` ADD KEY `idx_fee_order_range` (`order_min_minor_amount`, `order_max_minor_amount`)',
    'SELECT 1'
);
PREPARE zv_migration_stmt FROM @zv_migration_sql;
EXECUTE zv_migration_stmt;
DEALLOCATE PREPARE zv_migration_stmt;
