<?php

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('items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('location_id')->constrained()->cascadeOnDelete();
            $table->foreignId('category_id')->nullable()->constrained()->nullOnDelete();
            $table->unsignedBigInteger('import_row_id')->nullable();
            $table->string('name');
            $table->string('slug');
            $table->string('code')->nullable();
            $table->enum('tracking_mode', ['stock', 'unit'])->default('stock');
            $table->string('unit_of_measure')->default('pcs');
            $table->unsignedInteger('total_quantity')->default(0);
            $table->unsignedInteger('good_count')->default(0);
            $table->unsignedInteger('minor_damage_count')->default(0);
            $table->unsignedInteger('major_damage_count')->default(0);
            $table->text('notes')->nullable();
            $table->date('last_checked_at')->nullable();
            $table->string('qr_token')->nullable()->unique();
            $table->boolean('is_active')->default(true);
            $table->timestamps();

            $table->unique(['location_id', 'name']);
        });
    }

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