<?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('service_orders', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->uuid('user_id'); // Customer who placed the order
            $table->uuid('vendor_service_id'); // Service being ordered
            $table->string('order_number')->unique(); // Order number
            $table->json('items'); // Order items (JSON)
            $table->decimal('total_amount', 10, 2); // Total order amount
            $table->decimal('vendor_amount', 10, 2); // Amount going to vendor
            $table->decimal('platform_fee', 10, 2)->default(0); // Platform commission
            $table->string('currency', 3)->default('NGN'); // Currency
            $table->enum('status', ['pending', 'confirmed', 'in_progress', 'completed', 'cancelled', 'refunded'])->default('pending');
            $table->timestamp('delivery_date')->nullable(); // Expected delivery date
            $table->text('delivery_address')->nullable(); // Delivery address
            $table->text('customer_notes')->nullable(); // Customer notes
            $table->text('vendor_notes')->nullable(); // Vendor notes
            $table->timestamp('paid_at')->nullable(); // Payment confirmation
            $table->timestamp('delivered_at')->nullable(); // Delivery confirmation
            $table->timestamps();
            $table->softDeletes();

            // Indexes
            $table->index(['user_id', 'status']);
            $table->index(['vendor_service_id', 'status']);
            $table->index('status');
            $table->index('created_at');

            // Foreign keys
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('vendor_service_id')->references('id')->on('vendor_services')->onDelete('cascade');
        });
    }

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