<?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
    {
        if (!Schema::hasTable('vendor_orders')) {
            return;
        }

        Schema::table('vendor_orders', function (Blueprint $table) {
            if (!Schema::hasColumn('vendor_orders', 'tracking_link')) {
                $table->string('tracking_link', 500)->nullable()->after('total_amount');
            }
            if (!Schema::hasColumn('vendor_orders', 'cancel_reason')) {
                $table->text('cancel_reason')->nullable()->after('tracking_link');
            }
            if (!Schema::hasColumn('vendor_orders', 'cancellation_requested_at')) {
                $table->timestamp('cancellation_requested_at')->nullable()->after('cancel_reason');
                $table->index('cancellation_requested_at');
            }
            if (!Schema::hasColumn('vendor_orders', 'cancellation_response')) {
                $table->string('cancellation_response', 20)->nullable()->after('cancellation_requested_at');
            }
        });
    }

    public function down(): void
    {
        if (!Schema::hasTable('vendor_orders')) {
            return;
        }

        Schema::table('vendor_orders', function (Blueprint $table) {
            if (Schema::hasColumn('vendor_orders', 'cancellation_requested_at')) {
                $table->dropIndex(['cancellation_requested_at']);
                $table->dropColumn('cancellation_requested_at');
            }
            if (Schema::hasColumn('vendor_orders', 'cancellation_response')) {
                $table->dropColumn('cancellation_response');
            }
            if (Schema::hasColumn('vendor_orders', 'cancel_reason')) {
                $table->dropColumn('cancel_reason');
            }
            if (Schema::hasColumn('vendor_orders', 'tracking_link')) {
                $table->dropColumn('tracking_link');
            }
        });
    }
};
