<?php

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

return new class extends Migration
{
    /**
     * Unified activity feed events table.
     *
     * Event types: upload | comment | favorite | award | follow
     * target_type: artwork | user
     */
    public function up(): void
    {
        Schema::create('activity_events', function (Blueprint $table): void {
            $table->id();
            $table->unsignedBigInteger('actor_id')->index();
            $table->string('type', 20)->index();       // upload|comment|favorite|award|follow
            $table->string('target_type', 20)->index(); // artwork|user
            $table->unsignedBigInteger('target_id')->index();
            $table->json('meta')->nullable();           // extra context (category, tag, etc.)
            $table->timestamp('created_at')->useCurrent()->index();

            // Composite indexes for feed queries
            $table->index(['type', 'created_at'], 'activity_events_type_created_idx');
            $table->index(['actor_id', 'created_at'], 'activity_events_actor_created_idx');
            $table->index(['target_type', 'target_id'], 'activity_events_target_idx');

            $table->foreign('actor_id')->references('id')->on('users')->cascadeOnDelete();
        });
    }

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