<?php

use App\Models\Item;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateItemsTable extends Migration
{

    public function up(): void
    {
        Schema::create('items', function (Blueprint $table) {
            $table->increments('id');
            $table->string('caption');
            $table->unsignedInteger('user_id');
            $table->unsignedInteger('category_id')->nullable(); // стоит убрать, здесь не должно быть
            $table->unsignedInteger('subcategory_id')->nullable();
            $table->longText('description')->nullable();
            $table->boolean('archive')->nullable()->default(false);
            $table->boolean('pictures')->default(false);
            $table->timestamps();

            $table->foreign('user_id')
                ->references('id')->on('users')
                ->onDelete('cascade');
            $table->foreign('category_id')
                ->references('id')->on('categories')
                ->onDelete('set null');
            $table->foreign('subcategory_id')
                ->references('id')->on('subcategories')
                ->onDelete('set null');
        });
    }

    public function down(): void
    {
        Schema::table('items', function (Blueprint $table) {
            $table->dropForeign(['user_id']);
            $table->dropForeign(['category_id']);
            $table->dropForeign(['subcategory_id']);
        });

        Schema::dropIfExists('items');
    }
}
