<?php

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

/**
 * Migration: Partnership and Collaboration Portal - Forum Replies Table
 * 
 * Stores replies to forum threads with threading support.
 * Supports mentions, reactions, and moderation.
 * 
 * @author SSHIA Development Team
 * @version 1.0.0
 */
return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('r_forum_replies', function (Blueprint $table) {
            // Primary Key
            $table->id();
            
            // Foreign Keys
            $table->foreignId('thread_id')
                  ->constrained('r_forum_threads')
                  ->onDelete('cascade')
                  ->comment('Associated thread');
            
            $table->foreignId('user_id')
                  ->constrained('users')
                  ->onDelete('cascade')
                  ->comment('Reply author');
            
            // Parent Reply (for nested replies)
            $table->foreignId('parent_id')
                  ->nullable()
                  ->constrained('r_forum_replies')
                  ->nullOnDelete()
                  ->comment('Parent reply (for nested comments)');
            
            // Content
            $table->text('content')
                  ->comment('Reply content');
            
            // Mentions
            $table->json('mentions')
                  ->nullable()
                  ->comment('Mentioned user IDs (JSON array)');
            
            // Status
            $table->enum('status', ['published', 'hidden', 'deleted', 'pending'])
                  ->default('published')
                  ->comment('Reply status');
            
            $table->boolean('is_approved')
                  ->default(true)
                  ->comment('Approved by moderator');
            
            // Helpful/Reactions
            $table->unsignedInteger('helpful_count')
                  ->default(0)
                  ->comment('Number of "helpful" reactions');
            
            $table->unsignedInteger('reactions_count')
                  ->default(0)
                  ->comment('Total reactions count');
            
            // Best Answer (for Q&A threads)
            $table->boolean('is_best_answer')
                  ->default(false)
                  ->comment('Marked as best answer');
            
            $table->foreignId('marked_best_by')
                  ->nullable()
                  ->constrained('users')
                  ->nullOnDelete()
                  ->comment('User who marked as best answer');
            
            // Edit Tracking
            $table->boolean('is_edited')
                  ->default(false)
                  ->comment('Reply has been edited');
            
            $table->timestamp('edited_at')
                  ->nullable()
                  ->comment('Last edit timestamp');
            
            $table->foreignId('edited_by')
                  ->nullable()
                  ->constrained('users')
                  ->nullOnDelete()
                  ->comment('User who edited');
            
            // IP Address for moderation
            $table->string('ip_address', 45)
                  ->nullable()
                  ->comment('IP address for spam detection');
            
            // Timestamps
            $table->timestamps();
            $table->softDeletes();
            
            // Indexes
            $table->index('thread_id');
            $table->index('user_id');
            $table->index('parent_id');
            $table->index('status');
            $table->index('is_best_answer');
            $table->index(['thread_id', 'status', 'created_at'], 'r_forum_replies_thread_index');
        });
    }

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