<?php

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('odds_items', function (Blueprint $table): void {
            $table->string('provider_code', 32)->default('GLOBAL')->after('version_id');
        });

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

        Schema::table('odds_items', function (Blueprint $table): void {
            $table->dropUnique('uk_odds_items_version_play_prize_currency_dimension');
            $table->index(['version_id', 'provider_code', 'play_code'], 'idx_odds_items_version_provider_play');
            $table->unique(
                ['version_id', 'provider_code', 'play_code', 'prize_scope', 'currency_code', 'dimension'],
                'uk_odds_items_version_provider_play_prize_currency_dimension'
            );
        });
    }

    public function down(): void
    {
        Schema::table('odds_items', function (Blueprint $table): void {
            $table->dropUnique('uk_odds_items_version_provider_play_prize_currency_dimension');
            $table->dropIndex('idx_odds_items_version_provider_play');
            $table->unique(
                ['version_id', 'play_code', 'prize_scope', 'currency_code', 'dimension'],
                'uk_odds_items_version_play_prize_currency_dimension'
            );
            $table->dropColumn('provider_code');
        });
    }
};
