<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('mall_items', function (Blueprint $table) {
            $table->id();
            $table->integer('vnum');
            $table->unsignedBigInteger('category_id');
            $table->integer('old_price')->nullable();
            $table->integer('price');
            $table->enum('pricing', ['CASH', 'MILEAGE'])->default('CASH');
            $table->integer('quantity');
            $table->text('image')->nullable();
            $table->text('description')->nullable();
            $table->enum('other', ['recommend', 'recommend_desc'])->nullable();

            $table->foreign('category_id')->references('id')->on('mall_categories');
        });

        // Populate the table data
        $data = File::json(database_path('data/mall_items.json'));
        \App\Models\Mall\MallItem::upsert($data, ['id']);
    }

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