<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('costing_details', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('costing_id');
            $table->unsignedBigInteger('item_id');
            $table->decimal('qty', 15, 3);
            $table->unsignedBigInteger('unit');
            $table->decimal('base_qty', 15, 3);
            $table->unsignedBigInteger('base_unit');
            $table->decimal('cost_per_unit', 15, 2);
            $table->timestamps();

            $table->foreign('costing_id')->references('id')->on('costings')->onDelete('cascade');
            $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');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('costing_details');
    }
};
