<?php

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

return new class extends Migration
{
    public function up(): void
    {
        if (!Schema::hasTable('knowledge_base')) {
            Schema::create('knowledge_base', function (Blueprint $table) {
                $table->id();
                $table->string('title');
                $table->string('normalized_key')->unique();
                $table->string('source_category', 40)->default('custom')->index();
                $table->text('question_patterns')->nullable();
                $table->string('tags')->nullable();
                $table->longText('answer')->nullable();
                $table->longText('answer_bn')->nullable();
                $table->longText('answer_en')->nullable();
                $table->unsignedInteger('priority')->default(0)->index();
                $table->boolean('is_active')->default(true)->index();
                $table->unsignedInteger('version')->default(1);
                $table->foreignId('created_by_admin_id')->nullable()->constrained('admins')->nullOnDelete();
                $table->foreignId('updated_by_admin_id')->nullable()->constrained('admins')->nullOnDelete();
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('offers')) {
            Schema::create('offers', function (Blueprint $table) {
                $table->id();
                $table->string('title');
                $table->string('slug')->unique();
                $table->text('description')->nullable();
                $table->text('title_bn')->nullable();
                $table->text('title_en')->nullable();
                $table->longText('description_bn')->nullable();
                $table->longText('description_en')->nullable();
                $table->date('start_date')->nullable()->index();
                $table->date('end_date')->nullable()->index();
                $table->boolean('is_active')->default(true)->index();
                $table->unsignedInteger('priority')->default(0)->index();
                $table->unsignedInteger('version')->default(1);
                $table->foreignId('created_by_admin_id')->nullable()->constrained('admins')->nullOnDelete();
                $table->foreignId('updated_by_admin_id')->nullable()->constrained('admins')->nullOnDelete();
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('faq')) {
            Schema::create('faq', function (Blueprint $table) {
                $table->id();
                $table->string('question');
                $table->string('normalized_key')->unique();
                $table->text('question_bn')->nullable();
                $table->text('question_en')->nullable();
                $table->longText('answer')->nullable();
                $table->longText('answer_bn')->nullable();
                $table->longText('answer_en')->nullable();
                $table->unsignedInteger('priority')->default(0)->index();
                $table->boolean('is_active')->default(true)->index();
                $table->unsignedInteger('version')->default(1);
                $table->foreignId('created_by_admin_id')->nullable()->constrained('admins')->nullOnDelete();
                $table->foreignId('updated_by_admin_id')->nullable()->constrained('admins')->nullOnDelete();
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('ai_corrections')) {
            Schema::create('ai_corrections', function (Blueprint $table) {
                $table->id();
                $table->string('question');
                $table->string('normalized_question')->unique();
                $table->text('question_bn')->nullable();
                $table->text('question_en')->nullable();
                $table->longText('corrected_answer')->nullable();
                $table->longText('corrected_answer_bn')->nullable();
                $table->longText('corrected_answer_en')->nullable();
                $table->text('notes')->nullable();
                $table->unsignedInteger('priority')->default(100)->index();
                $table->boolean('is_active')->default(true)->index();
                $table->boolean('is_approved')->default(false)->index();
                $table->unsignedInteger('version')->default(1);
                $table->foreignId('corrected_by_admin_id')->nullable()->constrained('admins')->nullOnDelete();
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('chat_messages')) {
            Schema::create('chat_messages', function (Blueprint $table) {
                $table->id();
                $table->foreignId('conversation_id')->nullable()->constrained('conversations')->nullOnDelete();
                $table->foreignId('message_id')->nullable()->constrained('messages')->nullOnDelete();
                $table->foreignId('customer_id')->nullable()->constrained('users')->nullOnDelete();
                $table->foreignId('user_info_id')->nullable()->constrained('user_infos')->nullOnDelete();
                $table->string('role', 20)->index();
                $table->longText('message');
                $table->string('language', 5)->nullable()->index();
                $table->json('meta')->nullable();
                $table->timestamps();
            });
        }

        if (!Schema::hasTable('chat_logs')) {
            Schema::create('chat_logs', function (Blueprint $table) {
                $table->id();
                $table->foreignId('conversation_id')->nullable()->constrained('conversations')->nullOnDelete();
                $table->foreignId('incoming_message_id')->nullable()->constrained('messages')->nullOnDelete();
                $table->foreignId('outgoing_message_id')->nullable()->constrained('messages')->nullOnDelete();
                $table->foreignId('customer_id')->nullable()->constrained('users')->nullOnDelete();
                $table->string('normalized_question')->nullable()->index();
                $table->longText('question')->nullable();
                $table->longText('reply')->nullable();
                $table->string('language', 5)->nullable()->index();
                $table->string('query_type', 40)->nullable()->index();
                $table->string('source_type', 40)->nullable()->index();
                $table->unsignedBigInteger('source_id')->nullable()->index();
                $table->string('matched_keyword')->nullable();
                $table->unsignedInteger('confidence_score')->default(0);
                $table->boolean('fallback_used')->default(false)->index();
                $table->boolean('is_corrected')->default(false)->index();
                $table->json('meta')->nullable();
                $table->timestamps();
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('chat_logs');
        Schema::dropIfExists('chat_messages');
        Schema::dropIfExists('ai_corrections');
        Schema::dropIfExists('faq');
        Schema::dropIfExists('offers');
        Schema::dropIfExists('knowledge_base');
    }
};
