<?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_categories table for organizing Q&A content.
     * Supports hierarchical categories with parent_id for nested structure.
     * 
     * @return void
     */
    public function up(): void
    {
        Schema::create('knowledge_base_categories', function (Blueprint $table) {
            // Primary key
            $table->id();

            // Category details - 191 char limit for MySQL 5.7 compatibility
            $table->string('name', 191);
            $table->string('slug', 191)->unique();
            $table->text('description')->nullable();

            // Parent category for hierarchical structure
            $table->foreignId('parent_id')
                ->nullable()
                ->constrained('knowledge_base_categories')
                ->onDelete('cascade');

            // Status and ordering
            $table->boolean('is_active')->default(true);
            $table->integer('sort_order')->default(0);

            // Icon and color for UI
            $table->string('icon', 50)->default('fa-folder');
            $table->string('color', 20)->default('blue');

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

            // Count for quick access
            $table->integer('questions_count')->default(0);

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

            // Indexes for performance
            $table->index('parent_id');
            $table->index('is_active');
            $table->index('sort_order');
            $table->index('created_at');
            $table->index(['parent_id', 'is_active', 'sort_order']);
        });
    }

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