<?php

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

/**
 * Create answer_helpful_votes table
 * 
 * Tracks which users have marked a knowledge base answer as helpful.
 * Prevents duplicate votes per user per answer.
 */
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('answer_helpful_votes', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('answer_id');
            $table->unsignedBigInteger('user_id');
            $table->timestamp('created_at')->useCurrent();

            $table->foreign('answer_id')
                ->references('id')
                ->on('knowledge_base_answers')
                ->onDelete('cascade');

            $table->foreign('user_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade');

            // One vote per user per answer
            $table->unique(['answer_id', 'user_id']);

            $table->index('answer_id');
            $table->index('user_id');
        });
    }

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