<?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
    {
        Schema::create('content_moderation_findings', function (Blueprint $table) {
            $table->id();
            $table->string('content_type', 50)->index();          // artwork_comment, artwork_description
            $table->unsignedBigInteger('content_id')->index();     // id of the source record
            $table->unsignedBigInteger('artwork_id')->nullable()->index();
            $table->unsignedBigInteger('user_id')->nullable()->index();
            $table->string('status', 30)->default('pending')->index(); // pending, reviewed_safe, confirmed_spam, ignored, resolved
            $table->string('severity', 20)->default('low')->index();   // low, medium, high, critical
            $table->unsignedSmallInteger('score')->default(0);
            $table->string('content_hash', 64);                    // sha256 of normalised content
            $table->string('scanner_version', 20)->default('1.0');
            $table->json('reasons_json')->nullable();
            $table->json('matched_links_json')->nullable();
            $table->json('matched_domains_json')->nullable();
            $table->json('matched_keywords_json')->nullable();
            $table->text('content_snapshot')->nullable();
            $table->unsignedBigInteger('reviewed_by')->nullable();
            $table->timestamp('reviewed_at')->nullable();
            $table->string('action_taken', 50)->nullable();
            $table->text('admin_notes')->nullable();
            $table->timestamps();

            // Prevent duplicate findings for unchanged content
            $table->unique(['content_type', 'content_id', 'content_hash'], 'cmf_content_unique');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('content_moderation_findings');
    }
};
