<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        if (Schema::hasTable('app_settings')) {
            Schema::table('app_settings', function (Blueprint $table) {
                if (!Schema::hasColumn('app_settings', 'passenger_service_taxi_enabled')) {
                    $table->boolean('passenger_service_taxi_enabled')->default(true);
                }
                if (!Schema::hasColumn('app_settings', 'passenger_service_food_enabled')) {
                    $table->boolean('passenger_service_food_enabled')->default(true);
                }
                if (!Schema::hasColumn('app_settings', 'passenger_service_gas_enabled')) {
                    $table->boolean('passenger_service_gas_enabled')->default(true);
                }
                if (!Schema::hasColumn('app_settings', 'services_banner_enabled')) {
                    $table->boolean('services_banner_enabled')->default(false);
                }
                if (!Schema::hasColumn('app_settings', 'services_banner_title')) {
                    $table->string('services_banner_title')->nullable();
                }
                if (!Schema::hasColumn('app_settings', 'services_banner_subtitle')) {
                    $table->text('services_banner_subtitle')->nullable();
                }
                if (!Schema::hasColumn('app_settings', 'services_banner_body')) {
                    $table->text('services_banner_body')->nullable();
                }
                if (!Schema::hasColumn('app_settings', 'services_banner_cta_label')) {
                    $table->string('services_banner_cta_label', 120)->nullable();
                }
                if (!Schema::hasColumn('app_settings', 'global_completed_orders_count')) {
                    $table->unsignedBigInteger('global_completed_orders_count')->default(0);
                }
                if (!Schema::hasColumn('app_settings', 'wheel_milestone_interval')) {
                    $table->unsignedInteger('wheel_milestone_interval')->default(250);
                }
                if (!Schema::hasColumn('app_settings', 'wheel_winning_segment')) {
                    $table->unsignedTinyInteger('wheel_winning_segment')->default(12);
                }
                if (!Schema::hasColumn('app_settings', 'referral_withdraw_min_invitees')) {
                    $table->unsignedSmallInteger('referral_withdraw_min_invitees')->default(60);
                }
                if (!Schema::hasColumn('app_settings', 'referral_withdraw_min_orders_per_invitee')) {
                    $table->unsignedTinyInteger('referral_withdraw_min_orders_per_invitee')->default(10);
                }
            });
        }

        foreach (['food_orders', 'gas_orders', 'taxi_requests'] as $tbl) {
            if (!Schema::hasTable($tbl)) {
                continue;
            }
            Schema::table($tbl, function (Blueprint $table) use ($tbl) {
                if (!Schema::hasColumn($tbl, 'wheel_milestone_pending')) {
                    $table->boolean('wheel_milestone_pending')->default(false);
                }
                if (!Schema::hasColumn($tbl, 'wheel_promo_fee_amount')) {
                    $table->decimal('wheel_promo_fee_amount', 12, 2)->nullable();
                }
                if (!Schema::hasColumn($tbl, 'wheel_result')) {
                    $table->unsignedTinyInteger('wheel_result')->nullable();
                }
                if (!Schema::hasColumn($tbl, 'wheel_spun_at')) {
                    $table->timestamp('wheel_spun_at')->nullable();
                }
            });
        }
    }

    public function down(): void
    {
        if (Schema::hasTable('app_settings')) {
            Schema::table('app_settings', function (Blueprint $table) {
                foreach ([
                    'passenger_service_taxi_enabled',
                    'passenger_service_food_enabled',
                    'passenger_service_gas_enabled',
                    'services_banner_enabled',
                    'services_banner_title',
                    'services_banner_subtitle',
                    'services_banner_body',
                    'services_banner_cta_label',
                    'global_completed_orders_count',
                    'wheel_milestone_interval',
                    'wheel_winning_segment',
                    'referral_withdraw_min_invitees',
                    'referral_withdraw_min_orders_per_invitee',
                ] as $col) {
                    if (Schema::hasColumn('app_settings', $col)) {
                        $table->dropColumn($col);
                    }
                }
            });
        }

        foreach (['food_orders', 'gas_orders', 'taxi_requests'] as $tbl) {
            if (!Schema::hasTable($tbl)) {
                continue;
            }
            Schema::table($tbl, function (Blueprint $table) use ($tbl) {
                foreach (['wheel_milestone_pending', 'wheel_promo_fee_amount', 'wheel_result', 'wheel_spun_at'] as $col) {
                    if (Schema::hasColumn($tbl, $col)) {
                        $table->dropColumn($col);
                    }
                }
            });
        }
    }
};
