<?php

declare(strict_types=1);

use Hyperf\Database\Schema\Blueprint;
use Hypervel\Database\Migrations\Migration;
use Hypervel\Support\Facades\Schema;

return new class extends Migration {
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('shipments', function (Blueprint $table) {
            $table->id();
            $table->string('hashkey', 300)->unique();
            $table->unsignedBigInteger('transaction_id')->index();
            $table->unsignedBigInteger('store_id')->nullable()->index();
            $table->unsignedBigInteger('customer_id')->nullable()->index();
            $table->unsignedBigInteger('courier_id')->nullable()->index();
            $table->string('tracking_number')->unique()->nullable();
            $table->string('status', 50)->default('PENDING'); // PENDING, PICKED_UP, IN_TRANSIT, DELIVERED, FAILED, RETURNED
            $table->text('origin_address')->nullable();
            $table->text('destination_address')->nullable();
            $table->timestamp('estimated_delivery_date')->nullable();
            $table->timestamp('actual_delivery_date')->nullable();
            $table->decimal('shipping_fee', 15, 2)->default(0);
            $table->boolean('is_active')->default(true);
            $table->unsignedBigInteger('created_by')->nullable();
            $table->unsignedBigInteger('updated_by')->nullable();

            $table->foreign('transaction_id')->references('id')->on('global_transactions')->onDelete('cascade');
            $table->foreign('store_id')->references('id')->on('str')->onDelete('set null');
            $table->foreign('customer_id')->references('id')->on('cst')->onDelete('set null');
            $table->foreign('courier_id')->references('id')->on('couriers')->onDelete('set null');
            $table->foreign('created_by')->references('id')->on('users')->onDelete('set null');
            $table->foreign('updated_by')->references('id')->on('users')->onDelete('set null');

            $table->timestamps();
        });
    }

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