<?php

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

/**
 * Ranking Engine V2 — add shares_count, comments_count, ranking_score,
 * and engagement_velocity to artwork_stats.
 *
 * Also adds shares_24h, comments_24h, favourites_24h, and comments_7d
 * sliding-window counters needed for velocity calculation.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::table('artwork_stats', function (Blueprint $table) {
            // Denormalised counters (all-time)
            $table->unsignedBigInteger('comments_count')->default(0)->after('favorites');
            $table->unsignedBigInteger('shares_count')->default(0)->after('comments_count');

            // Windowed counters for velocity calculation
            $table->unsignedBigInteger('shares_24h')->default(0)->after('downloads_7d');
            $table->unsignedBigInteger('comments_24h')->default(0)->after('shares_24h');
            $table->unsignedBigInteger('favourites_24h')->default(0)->after('comments_24h');

            // V2 computed scores
            $table->double('ranking_score', 12, 4)->default(0)->after('rating_count');
            $table->double('engagement_velocity', 10, 4)->default(0)->after('ranking_score');

            // Indexes for sorting
            $table->index('ranking_score');
            $table->index('shares_count');
        });
    }

    public function down(): void
    {
        Schema::table('artwork_stats', function (Blueprint $table) {
            $table->dropIndex(['ranking_score']);
            $table->dropIndex(['shares_count']);
            $table->dropColumn([
                'comments_count',
                'shares_count',
                'shares_24h',
                'comments_24h',
                'favourites_24h',
                'ranking_score',
                'engagement_velocity',
            ]);
        });
    }
};
