<?php
declare(strict_types=1);

use App\Core\Database;

return function (Database $db): void {
    $cols = [
        'discount_type' => "ALTER TABLE special_discounts ADD COLUMN discount_type ENUM('percentage','fixed_amount','free_subscription') NOT NULL DEFAULT 'percentage' AFTER discount_percentage",
        'applies_to' => "ALTER TABLE special_discounts ADD COLUMN applies_to ENUM('membership_fee','subscription','all') NOT NULL DEFAULT 'membership_fee' AFTER discount_type",
        'effective_from' => "ALTER TABLE special_discounts ADD COLUMN effective_from DATE NULL DEFAULT NULL AFTER applies_to",
        'effective_to' => "ALTER TABLE special_discounts ADD COLUMN effective_to DATE NULL DEFAULT NULL AFTER effective_from",
        'fixed_amount' => "ALTER TABLE special_discounts ADD COLUMN fixed_amount DECIMAL(15,2) NULL DEFAULT NULL AFTER effective_to",
        'condition_type' => "ALTER TABLE special_discounts ADD COLUMN condition_type ENUM('none','min_payment','full_payment') NOT NULL DEFAULT 'none' AFTER fixed_amount",
        'condition_min_amount' => "ALTER TABLE special_discounts ADD COLUMN condition_min_amount DECIMAL(15,2) NULL DEFAULT NULL AFTER condition_type",
        'bonus_free_subscription_years' => "ALTER TABLE special_discounts ADD COLUMN bonus_free_subscription_years INT UNSIGNED NOT NULL DEFAULT 0 AFTER condition_min_amount",
    ];

    foreach ($cols as $col => $sql) {
        $exists = $db->selectOne(
            "SELECT 1 FROM information_schema.COLUMNS WHERE table_schema = DATABASE() AND table_name = 'special_discounts' AND column_name = ?",
            [$col]
        );
        if (!$exists) {
            $db->raw($sql);
        }
    }
};
