<?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('bet_providers', function (Blueprint $table): void {
            $table->string('short_code', 1)->nullable()->after('name');
        });

        DB::table('bet_providers')->orderBy('id')->get(['id', 'code'])->each(function (object $provider): void {
            DB::table('bet_providers')
                ->where('id', $provider->id)
                ->update(['short_code' => strtoupper(substr((string) $provider->code, 0, 1))]);
        });
    }

    public function down(): void
    {
        Schema::table('bet_providers', function (Blueprint $table): void {
            $table->dropColumn('short_code');
        });
    }
};
