<?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('user_interest_profiles', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->string('profile_version', 32)->default('profile-v1');
            $table->string('algo_version', 64)->default('clip-cosine-v1');
            $table->json('raw_scores_json')->nullable();
            $table->json('normalized_scores_json')->nullable();
            $table->decimal('total_weight', 14, 6)->default(0);
            $table->unsignedInteger('event_count')->default(0);
            $table->timestamp('last_event_at')->nullable()->index();
            $table->double('half_life_hours')->default(72);
            $table->uuid('updated_from_event_id')->nullable()->index();
            $table->timestamps();

            $table->unique(['user_id', 'profile_version', 'algo_version'], 'user_interest_profiles_unique_key');
            $table->index(['algo_version', 'updated_at'], 'user_interest_profiles_algo_updated_idx');
        });

        Schema::create('user_discovery_events', function (Blueprint $table): void {
            $table->id();
            $table->uuid('event_id')->unique();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->foreignId('artwork_id')->constrained('artworks')->cascadeOnDelete();
            $table->foreignId('category_id')->nullable()->constrained('categories')->nullOnDelete();
            $table->string('event_type', 16)->index(); // view|click|favorite|download
            $table->string('event_version', 32)->default('event-v1')->index();
            $table->string('algo_version', 64)->default('clip-cosine-v1')->index();
            $table->decimal('weight', 10, 4)->default(1.0);
            $table->date('event_date')->index();
            $table->timestamp('occurred_at')->nullable()->index();
            $table->json('meta')->nullable();
            $table->timestamps();

            $table->index(['user_id', 'event_date', 'event_type'], 'user_discovery_events_user_date_type_idx');
            $table->index(['user_id', 'algo_version', 'occurred_at'], 'user_discovery_events_user_algo_time_idx');
        });

        Schema::create('user_recommendation_cache', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->string('algo_version', 64);
            $table->string('cache_version', 32)->default('cache-v1');
            $table->json('recommendations_json')->nullable();
            $table->timestamp('generated_at')->nullable()->index();
            $table->timestamp('expires_at')->nullable()->index();
            $table->timestamps();

            $table->unique(['user_id', 'algo_version'], 'user_recommendation_cache_user_algo_unique');
            $table->index(['algo_version', 'expires_at'], 'user_recommendation_cache_algo_expiry_idx');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('user_recommendation_cache');
        Schema::dropIfExists('user_discovery_events');
        Schema::dropIfExists('user_interest_profiles');
    }
};
