<?php

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

/**
 * Migration: Partnership and Collaboration Portal - Forum Threads Table
 * 
 * Creates the forum threads table for the networking forum.
 * Supports categorized discussions among partners and mentors.
 * 
 * @author SSHIA Development Team
 * @version 1.0.0
 */
return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('r_forum_threads', function (Blueprint $table) {
            // Primary Key
            $table->id();
            
            // Author
            $table->foreignId('user_id')
                  ->constrained('users')
                  ->onDelete('cascade')
                  ->comment('Thread author');
            
            // Partner (optional - for partner-specific discussions)
            $table->foreignId('partner_id')
                  ->nullable()
                  ->constrained('r_partners')
                  ->nullOnDelete()
                  ->comment('Associated partner (for private partner threads)');
            
            // Thread Content
            $table->string('title', 191)
                  ->comment('Thread title');
            
            $table->string('slug', 191)
                  ->unique()
                  ->comment('URL-friendly identifier');
            
            $table->text('content')
                  ->comment('Thread content/body');
            
            // Category
            $table->enum('category', [
                'general', 
                'collaboration', 
                'resources', 
                'events', 
                'announcements', 
                'questions', 
                'best_practices', 
                'opportunities'
            ])->default('general')->comment('Thread category');
            
            // Visibility
            $table->enum('visibility', ['public', 'partners_only', 'mentors_only', 'private'])
                  ->default('public')
                  ->comment('Thread visibility/access level');
            
            // Status
            $table->enum('status', ['open', 'closed', 'archived', 'pinned'])
                  ->default('open')
                  ->comment('Thread status');
            
            $table->boolean('is_locked')
                  ->default(false)
                  ->comment('Locked for new replies');
            
            $table->boolean('is_pinned')
                  ->default(false)
                  ->comment('Pinned to top of forum');
            
            $table->boolean('is_featured')
                  ->default(false)
                  ->comment('Featured thread');
            
            // Statistics
            $table->unsignedInteger('views_count')
                  ->default(0)
                  ->comment('Number of views');
            
            $table->unsignedInteger('replies_count')
                  ->default(0)
                  ->comment('Number of replies');
            
            // Last Activity
            $table->foreignId('last_reply_user_id')
                  ->nullable()
                  ->constrained('users')
                  ->nullOnDelete()
                  ->comment('User who made last reply');
            
            $table->timestamp('last_reply_at')
                  ->nullable()
                  ->comment('Time of last reply');
            
            // Moderation
            $table->boolean('is_approved')
                  ->default(true)
                  ->comment('Approved by moderator');
            
            $table->foreignId('approved_by')
                  ->nullable()
                  ->constrained('users')
                  ->nullOnDelete()
                  ->comment('Moderator who approved');
            
            $table->timestamp('approved_at')
                  ->nullable()
                  ->comment('Approval timestamp');
            
            // Tags
            $table->json('tags')
                  ->nullable()
                  ->comment('Thread tags (JSON array)');
            
            // Timestamps
            $table->timestamps();
            $table->softDeletes();
            
            // Indexes
            $table->index('user_id');
            $table->index('partner_id');
            $table->index('category');
            $table->index('status');
            $table->index('visibility');
            $table->index('slug');
            $table->index(['status', 'is_pinned', 'last_reply_at'], 'r_forum_threads_listing_index');
        });
    }

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