<?php

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

return new class extends Migration
{
    public function up(): void
    {
        if (!Schema::hasTable('delivery_notes')) {
            Schema::create('delivery_notes', function (Blueprint $table) {
                $table->id();
                $table->string('number')->unique();
                $table->foreignId('order_id')->constrained()->cascadeOnDelete();
                $table->foreignId('vehicle_id')->nullable()->constrained('vehicles')->nullOnDelete();
                $table->string('recipient_name')->nullable();
                $table->string('recipient_phone')->nullable();
                $table->text('recipient_address')->nullable();
                $table->string('driver_name')->nullable();
                $table->string('driver_phone')->nullable();
                $table->string('vehicle_name')->nullable();
                $table->string('plate_number')->nullable();
                $table->date('delivery_date')->nullable();
                $table->string('status')->default('draft')->index();
                $table->text('notes')->nullable();
                $table->foreignId('created_by')->nullable()->constrained('users')->nullOnDelete();
                $table->timestamps();
                $table->index(['order_id', 'vehicle_id']);
            });
        }
    }

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