<?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('chat_logs')) {
            Schema::table('chat_logs', function (Blueprint $table) {
                if (!Schema::hasColumn('chat_logs', 'session_id')) {
                    $table->string('session_id', 120)->nullable()->index()->after('customer_id');
                }
                if (!Schema::hasColumn('chat_logs', 'matched_product_ids')) {
                    $table->json('matched_product_ids')->nullable()->after('source_id');
                }
                if (!Schema::hasColumn('chat_logs', 'is_wrong_answer_reported')) {
                    $table->boolean('is_wrong_answer_reported')->default(false)->index()->after('is_corrected');
                }
                if (!Schema::hasColumn('chat_logs', 'feedback_note')) {
                    $table->text('feedback_note')->nullable()->after('is_wrong_answer_reported');
                }
            });
        }

        if (Schema::hasTable('knowledge_base')) {
            Schema::table('knowledge_base', function (Blueprint $table) {
                if (!Schema::hasColumn('knowledge_base', 'source_url')) {
                    $table->text('source_url')->nullable()->after('source_category');
                }
                if (!Schema::hasColumn('knowledge_base', 'source_hash')) {
                    $table->string('source_hash', 64)->nullable()->index()->after('source_url');
                }
                if (!Schema::hasColumn('knowledge_base', 'synced_at')) {
                    $table->timestamp('synced_at')->nullable()->after('source_hash');
                }
            });
        }

        if (!Schema::hasTable('ai_product_indices')) {
            Schema::create('ai_product_indices', function (Blueprint $table) {
                $table->id();
                $table->foreignId('item_id')->unique()->constrained('items')->cascadeOnDelete();
                $table->string('sku', 191)->nullable()->index();
                $table->string('brand', 191)->nullable()->index();
                $table->string('category', 191)->nullable()->index();
                $table->longText('search_text')->nullable();
                $table->longText('normalized_text')->nullable();
                $table->json('embedding')->nullable();
                $table->json('payload')->nullable();
                $table->string('content_hash', 64)->nullable()->index();
                $table->timestamp('indexed_at')->nullable();
                $table->timestamps();
            });
        }
        if (Schema::hasTable('ai_product_indices') && !Schema::hasColumn('ai_product_indices', 'embedding')) {
            Schema::table('ai_product_indices', function (Blueprint $table) {
                $table->json('embedding')->nullable()->after('normalized_text');
            });
        }

        if (!Schema::hasTable('ai_product_image_indices')) {
            Schema::create('ai_product_image_indices', function (Blueprint $table) {
                $table->id();
                $table->foreignId('item_id')->constrained('items')->cascadeOnDelete();
                $table->string('image_key', 191)->nullable()->index();
                $table->text('image_url')->nullable();
                $table->text('local_path')->nullable();
                $table->char('perceptual_hash', 16)->nullable()->index();
                $table->json('embedding')->nullable();
                $table->unsignedSmallInteger('width')->nullable();
                $table->unsignedSmallInteger('height')->nullable();
                $table->string('content_hash', 64)->nullable()->index();
                $table->timestamp('indexed_at')->nullable();
                $table->timestamps();
                $table->unique(['item_id', 'image_key'], 'ai_product_image_item_key_unique');
            });
        }
    }

    public function down(): void
    {
        Schema::dropIfExists('ai_product_image_indices');
        Schema::dropIfExists('ai_product_indices');

        if (Schema::hasTable('knowledge_base')) {
            Schema::table('knowledge_base', function (Blueprint $table) {
                foreach (['source_url', 'source_hash', 'synced_at'] as $column) {
                    if (Schema::hasColumn('knowledge_base', $column)) {
                        $table->dropColumn($column);
                    }
                }
            });
        }

        if (Schema::hasTable('ai_product_indices') && Schema::hasColumn('ai_product_indices', 'embedding')) {
            Schema::table('ai_product_indices', function (Blueprint $table) {
                $table->dropColumn('embedding');
            });
        }

        if (Schema::hasTable('chat_logs')) {
            Schema::table('chat_logs', function (Blueprint $table) {
                foreach (['session_id', 'matched_product_ids', 'is_wrong_answer_reported', 'feedback_note'] as $column) {
                    if (Schema::hasColumn('chat_logs', $column)) {
                        $table->dropColumn($column);
                    }
                }
            });
        }
    }
};
