<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        if (Schema::hasTable('orders')) return;
        
        Schema::create('orders', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->foreignId('seller_id')->nullable()->constrained()->onDelete('set null');
            $table->unsignedBigInteger('referrer_user_id')->nullable();
            $table->string('referrer_code')->nullable();
            $table->string('order_number')->unique();
            $table->decimal('subtotal', 15, 2);
            $table->decimal('discount_amount', 15, 2)->default(0);
            $table->decimal('total', 15, 2);
            $table->decimal('total_amount', 15, 2)->nullable();
            $table->decimal('shipping_fee', 15, 2)->default(0);
            $table->decimal('seller_amount', 15, 2)->default(0);
            $table->string('status')->default('pending');
            $table->string('payment_status')->default('pending');
            $table->string('payment_method')->nullable();
            $table->string('payment_reference')->nullable();
            $table->string('order_type')->default('regular');
            $table->unsignedBigInteger('investment_product_id')->nullable();
            $table->text('notes')->nullable();
            
            // Customer Info
            $table->string('customer_name')->nullable();
            $table->string('customer_email')->nullable();
            $table->string('customer_phone')->nullable();
            
            // Shipping Info
            $table->text('shipping_address')->nullable();
            $table->string('shipping_city')->nullable();
            $table->string('shipping_country')->nullable();
            $table->string('shipping_district')->nullable();
            
            // Delivery Proof
            $table->string('delivery_proof')->nullable();
            $table->string('delivery_proof_status')->nullable();
            $table->text('delivery_proof_notes')->nullable();
            $table->timestamp('delivery_proof_submitted_at')->nullable();
            $table->timestamp('delivery_confirmed_at')->nullable();
            $table->unsignedBigInteger('delivery_confirmed_by')->nullable();
            $table->string('delivery_proof_for_customer')->nullable();
            $table->text('delivery_proof_message')->nullable();
            $table->timestamp('delivery_proof_transmitted_at')->nullable();
            
            // Timestamps
            $table->timestamp('shipped_at')->nullable();
            $table->timestamp('delivered_at')->nullable();
            $table->timestamps();
            
            // Indexes
            $table->index('seller_id');
            $table->index('status');
            $table->index('payment_status');
            $table->index('referrer_user_id');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('orders');
    }
};
