<?php

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

/**
 * Materialised ranking score table.
 *
 * Stores three pre-computed scores per artwork:
 *   score_trending  — time-decayed engagement (HL=72h)
 *   score_new_hot   — short novelty boost (HL=36h, first 48h emphasis)
 *   score_best      — slow-decay evergreen (HL=720h)
 *
 * Rebuilt hourly by RankComputeArtworkScoresJob.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('rank_artwork_scores', function (Blueprint $table): void {
            $table->unsignedBigInteger('artwork_id')->primary();
            $table->foreign('artwork_id')->references('id')->on('artworks')->cascadeOnDelete();

            $table->double('score_trending', 10, 6)->default(0)->index();
            $table->double('score_new_hot', 10, 6)->default(0)->index();
            $table->double('score_best', 10, 6)->default(0)->index();

            $table->string('model_version', 32)->default('rank_v1')->index();
            $table->timestamp('computed_at')->nullable()->index();
        });
    }

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