<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        if (!Schema::hasColumn('products', 'keywords')) {
            Schema::table('products', function (Blueprint $table) {
                $table->text('keywords')->nullable()->after('description');
            });
        }

        // Fulltext index for Smart Search
        try {
            DB::statement('ALTER TABLE products ADD FULLTEXT fulltext_index (name, description, keywords)');
        } catch (\Exception $e) {
            // Index might already exist or not supported, ignore for now to allow migration to pass
        }
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('products', function (Blueprint $table) {
            $table->dropIndex('fulltext_index');
            $table->dropColumn('keywords');
        });
    }
};
