<?php

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

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

            // Human-friendly auto-generated shipment id (e.g. SHIP-25-00000001)
            $table->string('shipment_id')->unique();

            // Basic details
            $table->string('origin')->nullable();
            $table->string('destination')->nullable();

            // Totals (calculated when creating shipment)
            $table->integer('total_ctn')->default(0)->comment('sum of CTN of selected orders');
            $table->integer('total_qty')->default(0)->comment('sum of qty of selected orders');
            $table->integer('total_ttl_qty')->default(0)->comment('sum of ttl_qty of selected orders');
            $table->decimal('total_amount', 16, 2)->default(0.00)->comment('sum of ttl_amount of selected orders');
            $table->decimal('total_cbm', 14, 3)->default(0.000)->comment('sum cbm');
            $table->decimal('total_ttl_cbm', 14, 3)->default(0.000)->comment('sum ttl cbm');
            $table->decimal('total_kg', 14, 3)->default(0.000)->comment('sum kg');
            $table->decimal('total_ttl_kg', 14, 3)->default(0.000)->comment('sum ttl kg');

            // status: pending (default), in_transit, dispatched, delivered, cancelled, etc.
            $table->string('status')->default('pending');

            // shipment date (admin can change)
            $table->date('shipment_date')->nullable();

            // optional meta (vehicle, driver etc)
            $table->json('meta')->nullable();

            $table->timestamps();

            // Indexes for fast filtering
            $table->index('shipment_id');
            $table->index('status');
            $table->index('shipment_date');
        });
    }

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