<?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
    {
        Schema::create('deliveries', function (Blueprint $table) {
            $table->id();
            $table->foreignId('subscription_id')->constrained()->onDelete('cascade');
            $table->foreignId('address_id')->constrained();
            $table->date('scheduled_date');
            $table->enum('time_slot', ['morning', 'afternoon', 'evening'])->default('morning');
            $table->enum('status', ['scheduled', 'preparing', 'out_for_delivery', 'delivered', 'skipped', 'failed'])
                  ->default('scheduled');
            $table->timestamp('delivered_at')->nullable();
            $table->text('driver_notes')->nullable();
            $table->string('tracking_code')->nullable();
            $table->timestamps();

            $table->index(['subscription_id', 'scheduled_date']);
            $table->index(['status', 'scheduled_date']);
        });

        // Meals selected for each delivery
        Schema::create('delivery_meals', function (Blueprint $table) {
            $table->id();
            $table->foreignId('delivery_id')->constrained()->onDelete('cascade');
            $table->foreignId('meal_id')->constrained();
            $table->integer('quantity')->default(1);
            $table->timestamps();
        });
    }

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