<?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', 'anonymized_at')) {
                $table->timestamp('anonymized_at')->nullable()->after('last_activity_at');
            }

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

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

    public function down(): void
    {
        if (app()->environment('local', 'testing')) {
            Schema::table('tenants', function (Blueprint $table) {
                if (Schema::hasColumn('tenants', 'deletion_warning_sent_at')) {
                    $table->dropColumn('deletion_warning_sent_at');
                }
                if (Schema::hasColumn('tenants', 'pending_deletion_at')) {
                    $table->dropColumn('pending_deletion_at');
                }
                if (Schema::hasColumn('tenants', 'anonymized_at')) {
                    $table->dropColumn('anonymized_at');
                }
            });
        }
    }
};
