<?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('quality_checks', function (Blueprint $table) {
            $table->id();
            $table->enum('context', ['grn', 'wo_inprocess', 'wo_final'])->index();
            $table->unsignedBigInteger('context_id')->index();
            $table->foreignId('item_id')->constrained('items')->restrictOnDelete();
            $table->string('lot_no')->nullable()->index();
            $table->enum('result', ['pass', 'fail', 'rework'])->index();
            $table->json('attributes')->nullable();
            $table->foreignId('checked_by_id')->constrained('users')->restrictOnDelete();
            $table->timestamps();

            $table->index(['context', 'context_id']);
            $table->index(['item_id', 'lot_no']);
        });
    }

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