<?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('photos', function (Blueprint $table) {
            if (! Schema::hasColumn('photos', 'moderation_notes')) {
                $table->text('moderation_notes')->nullable()->after('status');
            }

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

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

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

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