<?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('creator_milestones', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
            $table->string('type', 64);
            $table->timestamp('occurred_at');
            $table->unsignedSmallInteger('occurred_year')->nullable();
            $table->foreignId('related_artwork_id')->nullable()->constrained('artworks')->nullOnDelete();
            $table->boolean('is_public')->default(true);
            $table->unsignedTinyInteger('priority')->default(0);
            $table->json('payload_json')->nullable();
            $table->timestamp('computed_at')->nullable();
            $table->timestamps();

            $table->index(['user_id', 'occurred_at'], 'creator_milestones_user_occurred_idx');
            $table->index(['user_id', 'type'], 'creator_milestones_user_type_idx');
            $table->index(['user_id', 'is_public'], 'creator_milestones_user_public_idx');
        });
    }

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