<?php

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

/**
 * Create the manifests table.
 *
 * A Manifest represents a committed delivery plan for a specific vehicle
 * and (optionally) driver over a given time period. It is the output of
 * an Orchestrator commit and replaces the previous approach of loosely
 * grouping orders by driver_assigned_uuid.
 */
return new class extends Migration {
    public function up(): void
    {
        Schema::create('manifests', function (Blueprint $table) {
            $table->uuid('uuid')->primary();
            $table->string('public_id', 23)->unique()->nullable();
            $table->string('internal_id', 100)->nullable()->index();
            $table->char('company_uuid', 36)->nullable()->index();
            $table->char('driver_uuid', 36)->nullable()->index();
            $table->char('vehicle_uuid', 36)->nullable()->index();
            $table->string('status', 50)->default('draft')->index();
            // 'draft'       — generated by orchestrator, not yet accepted
            // 'active'      — driver accepted in Navigator
            // 'in_progress' — driver departed for first stop
            // 'completed'   — all stops completed
            // 'cancelled'   — cancelled before completion
            $table->date('scheduled_date')->nullable()->index();
            $table->timestamp('started_at')->nullable();
            $table->timestamp('completed_at')->nullable();
            $table->unsignedInteger('total_distance_m')->default(0);
            $table->unsignedInteger('total_duration_s')->default(0);
            $table->unsignedInteger('stop_count')->default(0);
            $table->text('notes')->nullable();
            $table->json('meta')->nullable();
            $table->timestamps();
            $table->softDeletes();
        });
    }

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