<?php

use App\Models\BetProvider;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('risk_pools', function (Blueprint $table): void {
            $table->string('provider_code', 32)->default(BetProvider::DEFAULT_CODE)->after('draw_id');
        });

        DB::table('risk_pools')->update([
            'provider_code' => BetProvider::DEFAULT_CODE,
        ]);

        Schema::table('risk_pools', function (Blueprint $table): void {
            $table->dropUnique('uk_risk_pools_draw_number');
            $table->dropIndex('idx_risk_pools_draw_soldout');
            $table->unique(['draw_id', 'provider_code', 'normalized_number'], 'uk_risk_pools_draw_provider_number');
            $table->index(['draw_id', 'provider_code', 'sold_out_status'], 'idx_risk_pools_draw_provider_soldout');
        });

        Schema::table('risk_pool_lock_logs', function (Blueprint $table): void {
            $table->string('provider_code', 32)->default(BetProvider::DEFAULT_CODE)->after('draw_id');
        });

        DB::table('risk_pool_lock_logs')->update([
            'provider_code' => BetProvider::DEFAULT_CODE,
        ]);

        Schema::table('risk_pool_lock_logs', function (Blueprint $table): void {
            $table->dropIndex('idx_risk_lock_logs_draw_number');
            $table->index(['draw_id', 'provider_code', 'normalized_number'], 'idx_risk_lock_logs_draw_provider_number');
        });

        Schema::table('risk_cap_items', function (Blueprint $table): void {
            $table->string('provider_code', 32)->default('GLOBAL')->after('version_id');
        });

        DB::table('risk_cap_items')->update([
            'provider_code' => 'GLOBAL',
        ]);

        Schema::table('risk_cap_items', function (Blueprint $table): void {
            $table->dropIndex('idx_risk_cap_items_lookup');
            $table->index(['version_id', 'provider_code', 'draw_id', 'normalized_number'], 'idx_risk_cap_items_version_provider_lookup');
        });
    }

    public function down(): void
    {
        Schema::table('risk_cap_items', function (Blueprint $table): void {
            $table->dropIndex('idx_risk_cap_items_version_provider_lookup');
            $table->index(['version_id', 'draw_id', 'normalized_number'], 'idx_risk_cap_items_lookup');
            $table->dropColumn('provider_code');
        });

        Schema::table('risk_pool_lock_logs', function (Blueprint $table): void {
            $table->dropIndex('idx_risk_lock_logs_draw_provider_number');
            $table->index(['draw_id', 'normalized_number'], 'idx_risk_lock_logs_draw_number');
            $table->dropColumn('provider_code');
        });

        Schema::table('risk_pools', function (Blueprint $table): void {
            $table->dropUnique('uk_risk_pools_draw_provider_number');
            $table->dropIndex('idx_risk_pools_draw_provider_soldout');
            $table->unique(['draw_id', 'normalized_number'], 'uk_risk_pools_draw_number');
            $table->index(['draw_id', 'sold_out_status'], 'idx_risk_pools_draw_soldout');
            $table->dropColumn('provider_code');
        });
    }
};
