<?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('orders', function (Blueprint $table) {
            $table->id();
            $table->string('order_number')->unique();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->foreignId('vendor_id')->constrained()->onDelete('cascade');
            $table->enum('type', ['ride', 'food', 'delivery', 'freelance']);
            $table->enum('status', ['pending', 'accepted', 'in_progress', 'completed', 'cancelled', 'disputed'])->default('pending');
            $table->decimal('subtotal', 15, 2)->default(0);
            $table->decimal('commission', 15, 2)->default(0);
            $table->decimal('delivery_fee', 15, 2)->default(0);
            $table->decimal('discount', 15, 2)->default(0);
            $table->decimal('total', 15, 2)->default(0);
            $table->enum('payment_method', ['wallet', 'cash', 'card'])->default('wallet');
            $table->enum('payment_status', ['pending', 'paid', 'refunded'])->default('pending');
            $table->text('pickup_address')->nullable();
            $table->text('delivery_address')->nullable();
            $table->decimal('pickup_lat', 10, 7)->nullable();
            $table->decimal('pickup_lng', 10, 7)->nullable();
            $table->decimal('delivery_lat', 10, 7)->nullable();
            $table->decimal('delivery_lng', 10, 7)->nullable();
            $table->text('notes')->nullable();
            $table->text('cancellation_reason')->nullable();
            $table->text('dispute_reason')->nullable();
            $table->text('dispute_resolution')->nullable();
            $table->foreignId('cancelled_by')->nullable()->constrained('admins')->onDelete('set null');
            $table->timestamp('accepted_at')->nullable();
            $table->timestamp('completed_at')->nullable();
            $table->timestamps();
            $table->softDeletes();
        });
    }

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