<?php

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

class CreateInvoiceItemsTable extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('invoice_items', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('invoice_id')->index();

            // Snapshot of order item fields (not editable)
            $table->text('description')->nullable();
            $table->integer('ctn')->default(0);
            $table->integer('qty')->default(0);
            $table->integer('ttl_qty')->default(0);
            $table->string('unit')->nullable();
            $table->decimal('price', 14, 2)->default(0.00);
            $table->decimal('ttl_amount', 14, 2)->default(0.00);

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

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

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

            $table->timestamps();

            // FK
            $table->foreign('invoice_id')->references('id')->on('invoices')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('invoice_items', function (Blueprint $table) {
            $table->dropForeign(['invoice_id']);
        });
        Schema::dropIfExists('invoice_items');
    }
}
