<?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('shop_deliveries')) {
            return;
        }

        $columns = ['created_by', 'updated_by', 'deleted_by'];
        $columnsToDrop = [];

        foreach ($columns as $column) {
            if (Schema::hasColumn('shop_deliveries', $column)) {
                $columnsToDrop[] = $column;
            }
        }

        if ($columnsToDrop) {
            Schema::table('shop_deliveries', function (Blueprint $table) use ($columnsToDrop) {
                $table->dropColumn($columnsToDrop);
            });
        }

        Schema::table('shop_deliveries', function (Blueprint $table) {
            $table->unsignedBigInteger('created_by')->nullable()->after('event_date_end');
            $table->unsignedBigInteger('updated_by')->nullable()->after('created_by');
            $table->unsignedBigInteger('deleted_by')->nullable()->after('updated_by');
        });
    }

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

        Schema::table('shop_deliveries', function (Blueprint $table) {
            $table->dropColumn(['created_by', 'updated_by', 'deleted_by']);
        });
    }
};
