<?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::table('orders', function (Blueprint $table) {
            if (!Schema::hasColumn('orders', 'delivery_proof')) {
                if (!Schema::hasColumn('orders', 'delivery_proof')) { $table->string('delivery_proof')->nullable(); }
            }
            if (!Schema::hasColumn('orders', 'delivery_proof_status')) {
                if (!Schema::hasColumn('orders', 'delivery_proof_status')) { $table->string('delivery_proof_status')->default('none'); }
            }
            if (!Schema::hasColumn('orders', 'delivery_proof_notes')) {
                if (!Schema::hasColumn('orders', 'delivery_proof_notes')) { $table->text('delivery_proof_notes')->nullable(); }
            }
            if (!Schema::hasColumn('orders', 'delivery_proof_submitted_at')) {
                if (!Schema::hasColumn('orders', 'delivery_proof_submitted_at')) { $table->timestamp('delivery_proof_submitted_at')->nullable(); }
            }
            if (!Schema::hasColumn('orders', 'delivery_confirmed_at')) {
                if (!Schema::hasColumn('orders', 'delivery_confirmed_at')) { $table->timestamp('delivery_confirmed_at')->nullable(); }
            }
            if (!Schema::hasColumn('orders', 'delivery_confirmed_by')) {
                if (!Schema::hasColumn('orders', 'delivery_confirmed_by')) { $table->unsignedBigInteger('delivery_confirmed_by')->nullable(); }
            }
            if (!Schema::hasColumn('orders', 'seller_id')) {
                if (!Schema::hasColumn('orders', 'seller_id')) { $table->unsignedBigInteger('seller_id')->nullable(); }
            }
            if (!Schema::hasColumn('orders', 'shipped_at')) {
                if (!Schema::hasColumn('orders', 'shipped_at')) { $table->timestamp('shipped_at')->nullable(); }
            }
            if (!Schema::hasColumn('orders', 'delivered_at')) {
                if (!Schema::hasColumn('orders', 'delivered_at')) { $table->timestamp('delivered_at')->nullable(); }
            }
        });
    }

    public function down(): void
    {
        Schema::table('orders', function (Blueprint $table) {
            $columns = ['delivery_proof', 'delivery_proof_status', 'delivery_proof_notes', 
                'delivery_proof_submitted_at', 'delivery_confirmed_at', 'delivery_confirmed_by',
                'seller_id', 'shipped_at', 'delivered_at'];
            foreach ($columns as $col) {
                if (Schema::hasColumn('orders', $col)) {
                    $table->dropColumn($col);
                }
            }
        });
    }
};


