<?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->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->json('address_snapshot');
            $table->foreignId('coupon_id')->nullable()->constrained()->onDelete('set null');
            $table->decimal('subtotal', 10, 2);
            $table->decimal('discount_total', 10, 2)->default(0);
            $table->decimal('shipping_total', 10, 2)->default(0);
            $table->decimal('total_payable', 10, 2);
            $table->enum('payment_status', ['pending', 'paid', 'failed', 'refunded'])->default('pending');
            $table->enum('status', ['placed', 'confirmed', 'packed', 'shipped', 'delivered', 'cancelled'])->default('placed');
            $table->string('razorpay_order_id', 191)->nullable();
            $table->string('razorpay_payment_id', 191)->nullable();
            $table->string('razorpay_signature', 191)->nullable();
            $table->string('delhivery_awb', 191)->nullable();
            $table->string('delhivery_label_url', 191)->nullable();
            $table->timestamp('placed_at')->nullable();
            $table->timestamps();

            $table->index('user_id');
            $table->index('razorpay_order_id');
            $table->index('delhivery_awb');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('orders');
    }
};
