<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('tenant_feedback', function (Blueprint $table) {
            if (! Schema::hasColumn('tenant_feedback', 'status')) {
                $table->string('status', 32)->default('pending')->after('message');
                $table->index('status', 'tenant_feedback_status_index');
            }

            if (! Schema::hasColumn('tenant_feedback', 'moderation_notes')) {
                $table->text('moderation_notes')->nullable()->after('status');
            }

            if (! Schema::hasColumn('tenant_feedback', 'moderated_at')) {
                $table->timestamp('moderated_at')->nullable()->after('moderation_notes');
            }

            if (! Schema::hasColumn('tenant_feedback', 'moderated_by')) {
                $table->foreignId('moderated_by')
                    ->nullable()
                    ->after('moderated_at')
                    ->constrained('users')
                    ->nullOnDelete();
            }
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('tenant_feedback', function (Blueprint $table) {
            if (Schema::hasColumn('tenant_feedback', 'moderated_by')) {
                $table->dropConstrainedForeignId('moderated_by');
            }

            foreach (['moderated_at', 'moderation_notes', 'status'] as $column) {
                if (Schema::hasColumn('tenant_feedback', $column)) {
                    $table->dropColumn($column);
                }
            }

        });
    }
};
