<?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
    {
        $fallback = config('app.fallback_locale', 'en');

        Schema::table('games', function (Blueprint $table) {
            $table->json('name_json')->nullable()->after('name');
            $table->json('description_json')->nullable()->after('description');
        });
        foreach (DB::table('games')->get() as $game) {
            $name = is_string($game->name) ? json_encode([$fallback => $game->name]) : $game->name;
            $description = $game->description
                ? (is_string($game->description) ? json_encode([$fallback => $game->description]) : $game->description)
                : null;
            DB::table('games')->where('id', $game->id)->update(['name_json' => $name, 'description_json' => $description]);
        }
        Schema::table('games', function (Blueprint $table) {
            $table->dropColumn(['name', 'description']);
        });
        Schema::table('games', function (Blueprint $table) {
            $table->renameColumn('name_json', 'name');
            $table->renameColumn('description_json', 'description');
        });

        // Courts: same process
        Schema::table('courts', function (Blueprint $table) {
            $table->json('name_json')->nullable()->after('name');
            $table->json('description_json')->nullable()->after('description');
        });
        foreach (DB::table('courts')->get() as $court) {
            $name = is_string($court->name) ? json_encode([$fallback => $court->name]) : $court->name;
            $description = $court->description
                ? (is_string($court->description) ? json_encode([$fallback => $court->description]) : $court->description)
                : null;
            DB::table('courts')->where('id', $court->id)->update(['name_json' => $name, 'description_json' => $description]);
        }
        Schema::table('courts', function (Blueprint $table) {
            $table->dropColumn(['name', 'description']);
        });
        Schema::table('courts', function (Blueprint $table) {
            $table->renameColumn('name_json', 'name');
            $table->renameColumn('description_json', 'description');
        });
    }

    public function down(): void
    {
        $fallback = config('app.fallback_locale', 'en');

        Schema::table('games', function (Blueprint $table) {
            $table->string('name_old')->nullable()->after('name');
            $table->text('description_old')->nullable()->after('description');
        });
        foreach (DB::table('games')->get() as $game) {
            $nameData = json_decode($game->name, true);
            $descData = $game->description ? json_decode($game->description, true) : null;
            DB::table('games')->where('id', $game->id)->update([
                'name_old' => $nameData[$fallback] ?? $nameData['en'] ?? '',
                'description_old' => $descData ? ($descData[$fallback] ?? $descData['en'] ?? '') : null,
            ]);
        }
        Schema::table('games', function (Blueprint $table) {
            $table->dropColumn(['name', 'description']);
            $table->renameColumn('name_old', 'name');
            $table->renameColumn('description_old', 'description');
        });

        Schema::table('courts', function (Blueprint $table) {
            $table->string('name_old')->nullable()->after('name');
            $table->text('description_old')->nullable()->after('description');
        });
        foreach (DB::table('courts')->get() as $court) {
            $nameData = json_decode($court->name, true);
            $descData = $court->description ? json_decode($court->description, true) : null;
            DB::table('courts')->where('id', $court->id)->update([
                'name_old' => $nameData[$fallback] ?? $nameData['en'] ?? '',
                'description_old' => $descData ? ($descData[$fallback] ?? $descData['en'] ?? '') : null,
            ]);
        }
        Schema::table('courts', function (Blueprint $table) {
            $table->dropColumn(['name', 'description']);
            $table->renameColumn('name_old', 'name');
            $table->renameColumn('description_old', 'description');
        });
    }
};
