<?php


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

class CreateOrderItemsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('order_items', function (Blueprint $table) {
            $table->bigIncrements('id');

            // Link to orders table (parent order)
            $table->foreignId('order_id')->constrained('orders')->onDelete('cascade');

            // Sub-order / line item fields
            $table->string('description')->nullable();
            $table->integer('ctn')->nullable()->default(0);
            $table->integer('qty')->nullable()->default(0);
            $table->integer('ttl_qty')->nullable()->default(0);

            $table->string('unit')->nullable();

            // financials & measurements
            $table->decimal('price', 14, 2)->nullable()->default(0.00);
            $table->decimal('ttl_amount', 16, 2)->nullable()->default(0.00);

            $table->decimal('cbm', 12, 3)->nullable()->default(0.000);
            $table->decimal('ttl_cbm', 14, 3)->nullable()->default(0.000);

            $table->decimal('kg', 12, 3)->nullable()->default(0.000);
            $table->decimal('ttl_kg', 14, 3)->nullable()->default(0.000);

            $table->string('shop_no')->nullable();

            // optional extra data (json for extensibility)
            $table->json('meta')->nullable();

            $table->timestamps();

            // Indexes for common queries
            $table->index('order_id');
            $table->index('ctn');
            $table->index('qty');
        });
    }

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