<?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()->cascadeOnDelete();

            $table->enum('status', [
                'pending',
                'paid',
                'failed',
                'awaiting_payment',
                'processing',
                'shipped',
                'delivered',
                'cancelled',
            ])->default('pending');

            // Financial
            $table->unsignedBigInteger('subtotal')->default(0);
            $table->unsignedBigInteger('product_discount')->default(0);
            $table->string('coupon_code')->nullable();
            $table->unsignedBigInteger('coupon_discount')->default(0);
            $table->unsignedBigInteger('shipping_cost')->default(0);
            $table->unsignedBigInteger('wallet_used')->default(0);
            $table->unsignedBigInteger('total_amount')->default(0);
            $table->unsignedBigInteger('payable_amount')->default(0);

            // Shipping
            $table->foreignId('shipping_method_id')->nullable()->constrained('shipping_methods')->nullOnDelete();
            $table->string('shipping_method_name')->nullable();
            $table->string('shipping_method_type')->nullable();

            // Address
            $table->foreignId('address_id')->nullable()->constrained('user_addresses')->nullOnDelete();
            $table->json('address_snapshot')->nullable();

            // Payment
            $table->string('payment_method')->nullable();
            $table->string('payment_method_name')->nullable();

            $table->timestamp('paid_at')->nullable();
            $table->text('notes')->nullable();

            $table->timestamps();
        });
    }

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