<?php

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

return new class extends Migration
{
    public function up(): void
    {
        Schema::table('worlds', function (Blueprint $table): void {
            $table->string('participation_mode', 32)->default('closed')->after('accepts_submissions');
            $table->boolean('allow_readd_after_removal')->default(true)->after('community_section_enabled');

            $table->index(['participation_mode', 'submission_starts_at', 'submission_ends_at'], 'worlds_participation_mode_window_idx');
        });

        DB::table('worlds')->orderBy('id')->chunkById(250, function ($rows): void {
            foreach ($rows as $row) {
                DB::table('worlds')
                    ->where('id', (int) $row->id)
                    ->update([
                        'participation_mode' => (bool) $row->accepts_submissions ? 'manual_approval' : 'closed',
                    ]);
            }
        });

        Schema::table('world_submissions', function (Blueprint $table): void {
            $table->boolean('is_featured')->default(false)->after('status');
            $table->string('mode_snapshot', 32)->nullable()->after('is_featured');
            $table->text('moderation_reason')->nullable()->after('reviewer_note');
            $table->timestamp('removed_at')->nullable()->after('reviewed_at');
            $table->timestamp('blocked_at')->nullable()->after('removed_at');
            $table->timestamp('featured_at')->nullable()->after('blocked_at');

            $table->index(['world_id', 'status', 'is_featured'], 'world_submissions_world_status_featured_idx');
        });

        DB::table('world_submissions')->orderBy('id')->chunkById(250, function ($rows): void {
            foreach ($rows as $row) {
                $status = (string) $row->status;
                $mappedStatus = match ($status) {
                    'approved', 'featured' => 'live',
                    'rejected' => 'removed',
                    default => 'pending',
                };

                $isFeatured = $status === 'featured';

                DB::table('world_submissions')
                    ->where('id', (int) $row->id)
                    ->update([
                        'status' => $mappedStatus,
                        'is_featured' => $isFeatured,
                        'moderation_reason' => $row->reviewer_note,
                        'removed_at' => $mappedStatus === 'removed' ? ($row->reviewed_at ?? now()) : null,
                        'featured_at' => $isFeatured ? ($row->reviewed_at ?? now()) : null,
                    ]);
            }
        });
    }

    public function down(): void
    {
        DB::table('world_submissions')->orderBy('id')->chunkById(250, function ($rows): void {
            foreach ($rows as $row) {
                $legacyStatus = match ((string) $row->status) {
                    'live' => (bool) $row->is_featured ? 'featured' : 'approved',
                    'removed', 'blocked' => 'rejected',
                    default => 'pending',
                };

                DB::table('world_submissions')
                    ->where('id', (int) $row->id)
                    ->update([
                        'status' => $legacyStatus,
                        'reviewer_note' => $row->moderation_reason,
                    ]);
            }
        });

        Schema::table('world_submissions', function (Blueprint $table): void {
            $table->dropIndex('world_submissions_world_status_featured_idx');
            $table->dropColumn(['is_featured', 'mode_snapshot', 'moderation_reason', 'removed_at', 'blocked_at', 'featured_at']);
        });

        Schema::table('worlds', function (Blueprint $table): void {
            $table->dropIndex('worlds_participation_mode_window_idx');
            $table->dropColumn(['participation_mode', 'allow_readd_after_removal']);
        });
    }
};