<?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('play_config_items', function (Blueprint $table): void {
            $table->string('category', 16)->nullable()->after('play_code');
            $table->unsignedTinyInteger('dimension')->nullable()->after('category');
            $table->string('bet_mode', 32)->nullable()->after('dimension');
            $table->string('display_name_zh', 64)->nullable()->after('bet_mode');
            $table->string('display_name_en', 64)->nullable()->after('display_name_zh');
            $table->string('display_name_ne', 64)->nullable()->after('display_name_en');
            $table->boolean('supports_multi_number')->default(false)->after('display_name_ne');
            $table->json('reserved_rule_json')->nullable()->after('supports_multi_number');
        });

        $playTypes = DB::table('play_types')
            ->select([
                'play_code',
                'category',
                'dimension',
                'bet_mode',
                'display_name_zh',
                'display_name_en',
                'display_name_ne',
                'supports_multi_number',
                'reserved_rule_json',
            ])
            ->get()
            ->keyBy('play_code');

        DB::table('play_config_items')
            ->select(['id', 'play_code'])
            ->orderBy('id')
            ->chunkById(200, function ($rows) use ($playTypes): void {
                foreach ($rows as $row) {
                    $pt = $playTypes->get($row->play_code);
                    if ($pt === null) {
                        continue;
                    }

                    DB::table('play_config_items')
                        ->where('id', $row->id)
                        ->update([
                            'category' => $pt->category,
                            'dimension' => $pt->dimension,
                            'bet_mode' => $pt->bet_mode,
                            'display_name_zh' => $pt->display_name_zh,
                            'display_name_en' => $pt->display_name_en,
                            'display_name_ne' => $pt->display_name_ne,
                            'supports_multi_number' => (bool) $pt->supports_multi_number,
                            'reserved_rule_json' => $pt->reserved_rule_json,
                        ]);
                }
            }, 'id');
    }

    public function down(): void
    {
        Schema::table('play_config_items', function (Blueprint $table): void {
            $table->dropColumn([
                'category',
                'dimension',
                'bet_mode',
                'display_name_zh',
                'display_name_en',
                'display_name_ne',
                'supports_multi_number',
                'reserved_rule_json',
            ]);
        });
    }
};
