<?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('tenants', function (Blueprint $table) {
            if (! Schema::hasColumn('tenants', 'notification_preferences')) {
                $table->json('notification_preferences')->nullable()->after('settings');
            }

            if (! Schema::hasColumn('tenants', 'credit_warning_sent_at')) {
                $table->timestamp('credit_warning_sent_at')->nullable()->after('notification_preferences');
            }

            if (! Schema::hasColumn('tenants', 'credit_warning_threshold')) {
                $table->unsignedInteger('credit_warning_threshold')->nullable()->after('credit_warning_sent_at');
            }
        });
    }

    public function down(): void
    {
        Schema::table('tenants', function (Blueprint $table) {
            foreach ([
                'credit_warning_threshold',
                'credit_warning_sent_at',
                'notification_preferences',
            ] as $column) {
                if (Schema::hasColumn('tenants', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }
};
