<?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
    {
        Schema::table('orders', function (Blueprint $table) {
            // Adicionar tracking_code após id
            $table->string('tracking_code', 20)->unique()->after('id')->nullable();
            
            // Adicionar coupon_code após payment_method
            $table->string('coupon_code', 50)->nullable()->after('payment_status');
            
            // Adicionar shipping_method_id após shipping_method
            $table->foreignId('shipping_method_id')->nullable()->after('shipping_method')->constrained('shipping_zones')->nullOnDelete();
            
            // Adicionar estimated_delivery
            $table->timestamp('estimated_delivery')->nullable()->after('delivered_at');
            
            // Adicionar cancellation_reason
            $table->text('cancellation_reason')->nullable()->after('cancelled_at');

            // Indexes
            $table->index('tracking_code');
            $table->index('coupon_code');
            $table->index('shipping_method_id');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('orders', function (Blueprint $table) {
            $table->dropIndex(['tracking_code']);
            $table->dropIndex(['coupon_code']);
            $table->dropForeign(['shipping_method_id']);
            $table->dropIndex(['shipping_method_id']);
            
            $table->dropColumn([
                'tracking_code',
                'coupon_code',
                'shipping_method_id',
                'estimated_delivery',
                'cancellation_reason',
            ]);
        });
    }
};
