<?php

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

// ══════════════════════════════════════════════════════════════════
//  InPractice — Forum Tables
//
//  Community forum where members share real work problems
//  and get guidance from other professionals.
//
//  Privacy:
//    posted_as_nickname → member can post under nickname only
//    visibility         → share with all, or selected hub only
//
//  Structure:
//    forum_posts   → the main post / question
//    forum_replies → responses to a post (threaded)
//    forum_likes   → likes on posts and replies
// ══════════════════════════════════════════════════════════════════

return new class extends Migration
{
    public function up(): void
    {
        // ── Forum Posts ────────────────────────────────────────
        Schema::create('forum_posts', function (Blueprint $table) {
            $table->id();

            $table->foreignId('user_id')
                  ->constrained('users')
                  ->cascadeOnDelete();

            $table->foreignId('hub_id')
                  ->constrained('hubs')
                  ->cascadeOnDelete();

            // ── Content ───────────────────────────────────────
            $table->string('title');
            $table->longText('body');
            $table->string('language', 2)->default('en');    // en | ar

            // ── Privacy ───────────────────────────────────────
            $table->boolean('posted_as_nickname')->default(true);  // show nickname or real name?
            $table->enum('visibility', [
                'all',               // visible to all members
                'hub',               // visible to hub members only
            ])->default('all');

            // ── Tags ──────────────────────────────────────────
            $table->json('tags')->nullable();                // topic tags for searchability

            // ── Status ────────────────────────────────────────
            $table->enum('status', [
                'active',
                'closed',            // no more replies allowed
                'removed',           // moderated by admin
            ])->default('active');

            // ── Counters (cached for performance) ─────────────
            $table->unsignedInteger('reply_count')->default(0);
            $table->unsignedInteger('like_count')->default(0);
            $table->unsignedInteger('view_count')->default(0);

            // ── Pinning ───────────────────────────────────────
            $table->boolean('is_pinned')->default(false);    // admin can pin important posts

            $table->timestamps();
        });

        // ── Forum Replies ──────────────────────────────────────
        Schema::create('forum_replies', function (Blueprint $table) {
            $table->id();

            $table->foreignId('forum_post_id')
                  ->constrained('forum_posts')
                  ->cascadeOnDelete();

            $table->foreignId('user_id')
                  ->constrained('users')
                  ->cascadeOnDelete();

            // ── Threading ─────────────────────────────────────
            // A reply can itself be a reply to another reply
            $table->foreignId('parent_reply_id')
                  ->nullable()
                  ->constrained('forum_replies')
                  ->cascadeOnDelete();

            // ── Content ───────────────────────────────────────
            $table->longText('body');
            $table->boolean('posted_as_nickname')->default(true);

            // ── Status ────────────────────────────────────────
            $table->enum('status', [
                'active',
                'removed',
            ])->default('active');

            // ── Marked as helpful ─────────────────────────────
            $table->boolean('is_best_answer')->default(false); // post owner can mark best answer

            $table->unsignedInteger('like_count')->default(0);

            $table->timestamps();
        });

        // ── Forum Likes ────────────────────────────────────────
        Schema::create('forum_likes', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')
                  ->constrained('users')
                  ->cascadeOnDelete();

            // Polymorphic — can like a post OR a reply
            $table->string('likeable_type');                 // forum_posts | forum_replies
            $table->unsignedBigInteger('likeable_id');
            $table->index(['likeable_type', 'likeable_id']);

            $table->timestamp('created_at')->useCurrent();

            // One like per user per item
            $table->unique(['user_id', 'likeable_type', 'likeable_id']);
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('forum_likes');
        Schema::dropIfExists('forum_replies');
        Schema::dropIfExists('forum_posts');
    }
};
