<?php

declare(strict_types=1);

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

/**
 * Phase 1 recommendation engine: user preference cache table.
 *
 * Stores pre-computed tag/category/creator affinity profiles so that
 * the "For You" feed can be generated without heavy joins on every request.
 *
 * TTL enforcement: the application checks `updated_at` + config TTL to decide
 * whether to rebuild.  A background job (or on-demand compute) refreshes stale rows.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('user_reco_profiles', function (Blueprint $table): void {
            $table->unsignedBigInteger('user_id')->primary();
            $table->json('top_tags_json')->nullable()->comment('Top tag slugs ordered by weighted score (up to 20)');
            $table->json('top_categories_json')->nullable()->comment('Top category slugs ordered by weight (up to 5)');
            $table->json('followed_creator_ids_json')->nullable()->comment('Followed creator IDs (up to 50)');
            $table->json('tag_weights_json')->nullable()->comment('Normalised tag slug → weight map');
            $table->json('category_weights_json')->nullable()->comment('Normalised category slug → weight map');
            $table->json('disliked_tag_ids_json')->nullable()->comment('Hidden/blocked tag slugs (future use)');
            $table->timestamps();

            $table->foreign('user_id')
                ->references('id')
                ->on('users')
                ->cascadeOnDelete();

            $table->index('updated_at', 'urp_updated_at_idx');
        });
    }

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