<?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) {
            $afterColumn = Schema::hasColumn('photos', 'moderated_by') ? 'moderated_by' : 'status';

            if (! Schema::hasColumn('photos', 'live_status')) {
                $table->string('live_status', 32)
                    ->default('none')
                    ->after($afterColumn);
                $table->index(['event_id', 'live_status'], 'photos_event_live_status_index');
            }

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

            if (! Schema::hasColumn('photos', 'live_approved_at')) {
                $table->timestamp('live_approved_at')
                    ->nullable()
                    ->after('live_submitted_at');
                $table->index(['event_id', 'live_status', 'live_approved_at'], 'photos_event_live_approved_index');
            }

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

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

            if (! Schema::hasColumn('photos', 'live_rejection_reason')) {
                $table->string('live_rejection_reason', 64)
                    ->nullable()
                    ->after('live_reviewed_by');
            }

            if (! Schema::hasColumn('photos', 'live_priority')) {
                $table->unsignedSmallInteger('live_priority')
                    ->default(0)
                    ->after('live_rejection_reason');
            }
        });
    }

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

            if (Schema::hasColumn('photos', 'live_rejection_reason')) {
                $table->dropColumn('live_rejection_reason');
            }

            if (Schema::hasColumn('photos', 'live_reviewed_by')) {
                $table->dropConstrainedForeignId('live_reviewed_by');
            }

            if (Schema::hasColumn('photos', 'live_reviewed_at')) {
                $table->dropColumn('live_reviewed_at');
            }

            if (Schema::hasColumn('photos', 'live_approved_at')) {
                $table->dropIndex('photos_event_live_approved_index');
                $table->dropColumn('live_approved_at');
            }

            if (Schema::hasColumn('photos', 'live_submitted_at')) {
                $table->dropColumn('live_submitted_at');
            }

            if (Schema::hasColumn('photos', 'live_status')) {
                $table->dropIndex('photos_event_live_status_index');
                $table->dropColumn('live_status');
            }
        });
    }
};
