<?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('artwork_metric_snapshots_hourly', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('artwork_id');
            $table->dateTime('bucket_hour')->comment('Hour-precision bucket, e.g. 2026-02-28 14:00:00');
            $table->unsignedBigInteger('views_count')->default(0);
            $table->unsignedBigInteger('downloads_count')->default(0);
            $table->unsignedBigInteger('favourites_count')->default(0);
            $table->unsignedBigInteger('comments_count')->default(0);
            $table->unsignedBigInteger('shares_count')->default(0);
            $table->timestamp('created_at')->useCurrent();

            $table->unique(['artwork_id', 'bucket_hour'], 'uq_artwork_bucket');
            $table->index('bucket_hour', 'idx_bucket_hour');
            $table->index(['artwork_id', 'bucket_hour'], 'idx_artwork_bucket');

            $table->foreign('artwork_id')
                ->references('id')
                ->on('artworks')
                ->cascadeOnDelete();
        });
    }

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