<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     * 
     * Creates the content_interactions table for tracking user engagement.
     * Supports likes, helpful votes, bookmarks, and other interactions.
     * 
     * @return void
     */
    public function up(): void
    {
        Schema::create('content_interactions', function (Blueprint $table) {
            // Primary key
            $table->id();

            // Polymorphic relationship for different content types
            $table->string('interactable_type', 191);
            $table->unsignedBigInteger('interactable_id');

            // User who interacted
            $table->foreignId('user_id')
                ->constrained('users')
                ->onDelete('cascade');

            // Interaction type
            $table->enum('interaction_type', [
                'like',
                'helpful',
                'not_helpful',
                'bookmark',
                'share',
                'report'
            ]);

            // Additional metadata
            $table->json('metadata')->nullable();

            // Timestamps
            $table->timestamps();

            // Unique constraint to prevent duplicate interactions
            $table->unique(
                ['interactable_type', 'interactable_id', 'user_id', 'interaction_type'],
                'content_interactions_unique'
            );

            // Indexes for performance
            $table->index(['interactable_type', 'interactable_id']);
            $table->index('user_id');
            $table->index('interaction_type');
            $table->index('created_at');
            $table->index(['interactable_type', 'interactable_id', 'interaction_type'], 'ci_type_id_interaction_idx');
        });
    }

    /**
     * Reverse the migrations.
     * 
     * @return void
     */
    public function down(): void
    {
        Schema::dropIfExists('content_interactions');
    }
};
