<?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 knowledge_base_questions table for structured Q&A content.
     * Questions are managed by authorized users (Managers+) and can have multiple answers.
     * 
     * @return void
     */
    public function up(): void
    {
        Schema::create('knowledge_base_questions', function (Blueprint $table) {
            // Primary key
            $table->id();

            // Question content - 191 char limit for MySQL 5.7 compatibility
            $table->string('title', 191);
            $table->string('slug', 191)->unique();
            $table->text('content');
            $table->text('excerpt')->nullable();

            // Foreign keys
            $table->foreignId('category_id')
                ->constrained('knowledge_base_categories')
                ->onDelete('cascade');
            $table->foreignId('user_id')
                ->constrained('users')
                ->onDelete('cascade');

            // Status management
            $table->enum('status', ['draft', 'published', 'archived', 'pending_review'])
                ->default('draft');
            $table->boolean('is_published')->default(false);
            $table->boolean('is_featured')->default(false);

            // Engagement metrics
            $table->integer('view_count')->default(0);
            $table->integer('answer_count')->default(0);
            $table->integer('helpful_count')->default(0);

            // Difficulty level for categorization
            $table->enum('difficulty', ['beginner', 'intermediate', 'advanced'])
                ->default('intermediate');

            // Tags for flexible categorization
            $table->json('tags')->nullable();

            // SEO metadata
            $table->string('meta_title', 191)->nullable();
            $table->text('meta_description')->nullable();
            $table->string('meta_keywords', 191)->nullable();

            // Structured data for SEO
            $table->json('schema_data')->nullable();

            // Timestamps
            $table->timestamps();
            $table->softDeletes();

            // Published timestamp
            $table->timestamp('published_at')->nullable();

            // Indexes for performance
            $table->index('category_id');
            $table->index('user_id');
            $table->index('status');
            $table->index('is_published');
            $table->index('is_featured');
            $table->index('difficulty');
            $table->index('created_at');
            $table->index('published_at');
            $table->index(['category_id', 'is_published', 'status']);
            $table->index(['is_published', 'status', 'published_at']);

            // Full-text index for search (MySQL 5.7 compatible)
            $table->fullText(['title', 'content']);
        });
    }

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