<?php

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

/**
 * Adds sliding-window view and download counters to artwork_stats.
 *
 * These columns accumulate between scheduled resets:
 *   views_24h     — incremented on every view event; zeroed nightly at 03:30
 *   views_7d      — incremented on every view event; zeroed weekly on Monday 03:30
 *   downloads_24h — recomputed nightly from artwork_downloads log (accurate)
 *   downloads_7d  — recomputed weekly from artwork_downloads log (accurate)
 *
 * TrendingService uses these instead of the all-time views/downloads totals.
 */
return new class extends Migration {
    public function up(): void
    {
        Schema::table('artwork_stats', function (Blueprint $table) {
            $table->unsignedBigInteger('views_24h')->default(0)->after('views');
            $table->unsignedBigInteger('views_7d')->default(0)->after('views_24h');
            $table->unsignedBigInteger('downloads_24h')->default(0)->after('downloads');
            $table->unsignedBigInteger('downloads_7d')->default(0)->after('downloads_24h');
        });
    }

    public function down(): void
    {
        Schema::table('artwork_stats', function (Blueprint $table) {
            $table->dropColumn(['views_24h', 'views_7d', 'downloads_24h', 'downloads_7d']);
        });
    }
};
