<?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('draw_result_batches', function (Blueprint $table): void {
            $table->dropUnique('uk_draw_result_batches_draw_version');
        });

        Schema::table('draw_result_batches', function (Blueprint $table): void {
            $table->string('provider_code', 32)->default(BetProvider::DEFAULT_CODE)->after('draw_id');
            $table->string('provider_name', 64)->default('Singapore')->after('provider_code');
            $table->unique(['draw_id', 'provider_code', 'result_version'], 'uk_draw_result_batches_draw_provider_version');
            $table->index(['draw_id', 'provider_code', 'status'], 'idx_draw_result_batches_draw_provider_status');
        });

        DB::table('draw_result_batches')->update([
            'provider_code' => BetProvider::DEFAULT_CODE,
            'provider_name' => 'Singapore',
        ]);
    }

    public function down(): void
    {
        Schema::table('draw_result_batches', function (Blueprint $table): void {
            $table->dropUnique('uk_draw_result_batches_draw_provider_version');
            $table->dropIndex('idx_draw_result_batches_draw_provider_status');
            $table->dropColumn(['provider_code', 'provider_name']);
        });

        Schema::table('draw_result_batches', function (Blueprint $table): void {
            $table->unique(['draw_id', 'result_version'], 'uk_draw_result_batches_draw_version');
        });
    }
};
