<?php

use App\Models\Game\Player\ShopExItem;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('shopex_item', function (Blueprint $table) {
            $table->unsignedInteger('shop_vnum');
            $table->unsignedInteger('item_vnum');

            $table->unsignedTinyInteger('count')->default(1);
            $table->unsignedInteger('price')->default(0);
            $table->unsignedInteger('price_vnum')->default(0);
            $table->enum('price_type', ['GOLD', 'SECONDCOIN', 'ITEM', 'EXP'])->default('GOLD');

            $table->unsignedInteger('socket0')->default(0);
            $table->unsignedInteger('socket1')->default(0);
            $table->unsignedInteger('socket2')->default(0);
            $table->unsignedInteger('socket3')->default(0);
            $table->unsignedInteger('socket4')->default(0);

            $table->tinyInteger('attrtype0')->default(0);
            $table->smallInteger('attrvalue0')->default(0);
            $table->tinyInteger('attrtype1')->default(0);
            $table->smallInteger('attrvalue1')->default(0);
            $table->tinyInteger('attrtype2')->default(0);
            $table->smallInteger('attrvalue2')->default(0);
            $table->tinyInteger('attrtype3')->default(0);
            $table->smallInteger('attrvalue3')->default(0);
            $table->tinyInteger('attrtype4')->default(0);
            $table->smallInteger('attrvalue4')->default(0);
            $table->tinyInteger('attrtype5')->default(0);
            $table->smallInteger('attrvalue5')->default(0);
            $table->tinyInteger('attrtype6')->default(0);
            $table->smallInteger('attrvalue6')->default(0);

            $table->unique(
                ['shop_vnum', 'item_vnum', 'count', 'price', 'price_vnum', 'price_type'],
                'vnum_unique'
            );
            $table->foreign('shop_vnum')->references('vnum')->on('shopex')->cascadeOnDelete();
        });

        // Populate the table data
        $data = File::json(database_path('data/shopex_item.json'));

        ShopExItem::upsert($data, ['shop_vnum', 'item_vnum']);
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('shopex_item');
    }
};
