<?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
    {
        if (Schema::hasTable('uploads')) {
            $missingUploadColumns = [
                'tags' => ! Schema::hasColumn('uploads', 'tags'),
                'license' => ! Schema::hasColumn('uploads', 'license'),
                'nsfw' => ! Schema::hasColumn('uploads', 'nsfw'),
                'is_scanned' => ! Schema::hasColumn('uploads', 'is_scanned'),
                'has_tags' => ! Schema::hasColumn('uploads', 'has_tags'),
                'published_at' => ! Schema::hasColumn('uploads', 'published_at'),
                'final_path' => ! Schema::hasColumn('uploads', 'final_path'),
            ];

            if (in_array(true, $missingUploadColumns, true)) {
                Schema::table('uploads', function (Blueprint $table) use ($missingUploadColumns): void {
                    if ($missingUploadColumns['tags']) {
                        $table->json('tags')->nullable();
                    }

                    if ($missingUploadColumns['license']) {
                        $table->string('license', 64)->nullable();
                    }

                    if ($missingUploadColumns['nsfw']) {
                        $table->boolean('nsfw')->default(false);
                    }

                    if ($missingUploadColumns['is_scanned']) {
                        $table->boolean('is_scanned')->default(false)->index();
                    }

                    if ($missingUploadColumns['has_tags']) {
                        $table->boolean('has_tags')->default(false)->index();
                    }

                    if ($missingUploadColumns['published_at']) {
                        $table->timestamp('published_at')->nullable()->index();
                    }

                    if ($missingUploadColumns['final_path']) {
                        $table->string('final_path')->nullable();
                    }
                });
            }
        }

        if (Schema::hasTable('forum_ai_logs')) {
            $missingForumAiColumns = [
                'firewall_score' => ! Schema::hasColumn('forum_ai_logs', 'firewall_score'),
                'bot_risk_score' => ! Schema::hasColumn('forum_ai_logs', 'bot_risk_score'),
            ];

            if (in_array(true, $missingForumAiColumns, true)) {
                Schema::table('forum_ai_logs', function (Blueprint $table) use ($missingForumAiColumns): void {
                    if ($missingForumAiColumns['firewall_score']) {
                        $table->unsignedSmallInteger('firewall_score')->default(0);
                    }

                    if ($missingForumAiColumns['bot_risk_score']) {
                        $table->unsignedSmallInteger('bot_risk_score')->default(0);
                    }
                });
            }
        }
    }

    public function down(): void
    {
        if (Schema::hasTable('forum_ai_logs')) {
            Schema::table('forum_ai_logs', function (Blueprint $table): void {
                $dropColumns = [];

                if (Schema::hasColumn('forum_ai_logs', 'firewall_score')) {
                    $dropColumns[] = 'firewall_score';
                }

                if (Schema::hasColumn('forum_ai_logs', 'bot_risk_score')) {
                    $dropColumns[] = 'bot_risk_score';
                }

                if ($dropColumns !== []) {
                    $table->dropColumn($dropColumns);
                }
            });
        }

        if (Schema::hasTable('uploads')) {
            Schema::table('uploads', function (Blueprint $table): void {
                $dropColumns = [];

                foreach (['tags', 'license', 'nsfw', 'is_scanned', 'has_tags', 'published_at', 'final_path'] as $column) {
                    if (Schema::hasColumn('uploads', $column)) {
                        $dropColumns[] = $column;
                    }
                }

                if ($dropColumns !== []) {
                    $table->dropColumn($dropColumns);
                }
            });
        }
    }
};
