<?php

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

/**
 * Ranked list cache table.
 *
 * Stores ordered artwork_id JSON arrays for each feed surface:
 *   scope_type : global | category | content_type
 *   scope_id   : 0 for global, foreign-key id for category / content_type
 *   list_type  : trending | new_hot | best
 *
 * Rebuilt hourly by RankBuildListsJob.
 * scope_id uses 0 as sentinel for "global" (avoids nullable unique-key pitfalls in MySQL).
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('rank_lists', function (Blueprint $table): void {
            $table->id();

            $table->string('scope_type', 32)->index();       // global | category | content_type
            $table->unsignedBigInteger('scope_id')->default(0)->index(); // 0 = global
            $table->string('list_type', 32)->index();        // trending | new_hot | best
            $table->string('model_version', 32)->default('rank_v1');

            $table->json('artwork_ids');                     // ordered list of ids
            $table->timestamp('computed_at')->nullable();

            $table->unique(
                ['scope_type', 'scope_id', 'list_type', 'model_version'],
                'rank_lists_scope_unique'
            );
        });
    }

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