<?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 chat_categories table for visitor category selection.
     * 
     * Requirements: 4.2, 4.3
     */
    public function up(): void
    {
        Schema::create('chat_categories', function (Blueprint $table) {
            // Primary key
            $table->id();

            // Category details (respecting 191 char limit for indexed columns)
            $table->string('name', 191)->comment('Category name displayed to visitors');
            $table->string('slug', 191)->unique()->comment('URL-friendly identifier');
            $table->text('description')->nullable()->comment('Category description');

            // Display order
            $table->integer('sort_order')->default(0)->comment('Display order (lower first)');

            // Icon for UI (FontAwesome class)
            $table->string('icon', 100)->default('fa-comments')->comment('FontAwesome icon class');

            // Color for UI distinction
            $table->string('color', 20)->default('#10b981')->comment('Hex color code for category');

            // Active status
            $table->boolean('is_active')
                ->default(true)
                ->comment('Whether category is available for selection');

            // Auto-assignment rules
            $table->foreignId('default_agent_id')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete()
                ->comment('Default agent for auto-assignment');

            // Expected response time (minutes)
            $table->integer('expected_response_time')->default(5)->comment('Expected initial response time in minutes');

            // Who manages this category
            $table->foreignId('created_by')
                ->nullable()
                ->constrained('users')
                ->nullOnDelete()
                ->comment('User who created the category');

            // Statistics
            $table->integer('conversations_count')->default(0)->comment('Total conversations in this category');

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

            // Indexes for performance
            $table->index('is_active');
            $table->index('sort_order');
            $table->index('slug');
        });
    }

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