<?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('purchase_requisition_makloon_details', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('purchase_requisition_makloon_id');
            $table->unsignedBigInteger('item_id');
            $table->unsignedBigInteger('unit');
            $table->decimal('qty', 25, 3);
            $table->string('base_unit');
            $table->decimal('base_qty', 20, 3);
            $table->decimal('qty_left', 25, 3);
            $table->text('description')->nullable();
            $table->boolean('is_cancel')->nullable();
            $table->text('cancel_notes')->nullable();
            $table->datetime('canceled_at')->nullable();
            $table->unsignedBigInteger('canceled_by')->nullable();
            $table->timestamps();
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();

            // Foreign key constraints
            $table->foreign('purchase_requisition_makloon_id')->references('id')->on('purchase_requisition_makloons')->onDelete('restrict');
            $table->foreign('item_id')->references('id')->on('items')->onDelete('restrict');
            $table->foreign('unit')->references('id')->on('item_units')->onDelete('restrict');
            $table->foreign(['item_id','base_qty'])->references(['item_id','conversion'])->on('item_details')->onDelete('restrict');
            $table->foreign('canceled_by')->references('id')->on('users')->onDelete('restrict');
            $table->foreign('created_by')->references('id')->on('users')->onDelete('restrict');
            $table->foreign('updated_by')->references('id')->on('users')->onDelete('restrict');
        });
    }

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