<?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('tenant_packages', function (Blueprint $table) {
            if (! Schema::hasColumn('tenant_packages', 'event_warning_sent_at')) {
                $table->timestamp('event_warning_sent_at')->nullable()->after('used_events');
            }

            if (! Schema::hasColumn('tenant_packages', 'event_warning_threshold')) {
                $table->decimal('event_warning_threshold', 5, 2)->nullable()->after('event_warning_sent_at');
            }

            if (! Schema::hasColumn('tenant_packages', 'event_limit_notified_at')) {
                $table->timestamp('event_limit_notified_at')->nullable()->after('event_warning_threshold');
            }

            if (! Schema::hasColumn('tenant_packages', 'expiry_warning_sent_at')) {
                $table->timestamp('expiry_warning_sent_at')->nullable()->after('event_limit_notified_at');
            }

            if (! Schema::hasColumn('tenant_packages', 'expired_notified_at')) {
                $table->timestamp('expired_notified_at')->nullable()->after('expiry_warning_sent_at');
            }
        });
    }

    public function down(): void
    {
        Schema::table('tenant_packages', function (Blueprint $table) {
            foreach ([
                'expired_notified_at',
                'expiry_warning_sent_at',
                'event_limit_notified_at',
                'event_warning_threshold',
                'event_warning_sent_at',
            ] as $column) {
                if (Schema::hasColumn('tenant_packages', $column)) {
                    $table->dropColumn($column);
                }
            }
        });
    }
};
