<?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()
    {
        if (!Schema::hasTable('egame_items')) {
            return;
        }

        if (Schema::hasColumn('egame_items', 'image_square')) {
            DB::table('egame_items')
                ->where('item_type', 'game')
                ->where(function ($query) {
                    $query->whereNull('logo')->orWhere('logo', '');
                })
                ->where('image_square', '<>', '')
                ->update(['logo' => DB::raw('image_square')]);
        }

        Schema::table('egame_items', function (Blueprint $table) {
            foreach (['image_circle', 'image_rectangle', 'image_square'] as $column) {
                if (Schema::hasColumn('egame_items', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }

    public function down()
    {
        if (!Schema::hasTable('egame_items')) {
            return;
        }

        Schema::table('egame_items', function (Blueprint $table) {
            if (!Schema::hasColumn('egame_items', 'image_circle')) {
                $table->string('image_circle', 500)->default('')->comment('圆形游戏图');
            }
            if (!Schema::hasColumn('egame_items', 'image_rectangle')) {
                $table->string('image_rectangle', 500)->default('')->comment('矩形游戏图');
            }
            if (!Schema::hasColumn('egame_items', 'image_square')) {
                $table->string('image_square', 500)->default('')->comment('方形游戏图');
            }
        });
    }
};
