<?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
    {
        Schema::table('faqs', function (Blueprint $table) {
            if (! Schema::hasColumn('faqs', 'question')) {
                $table->string('question', 500)->after('id');
            }
            if (! Schema::hasColumn('faqs', 'question_km')) {
                $table->string('question_km', 500)->nullable()->after('question');
            }
            if (! Schema::hasColumn('faqs', 'answer')) {
                $table->text('answer')->after('question_km');
            }
            if (! Schema::hasColumn('faqs', 'answer_km')) {
                $table->text('answer_km')->nullable()->after('answer');
            }
            if (! Schema::hasColumn('faqs', 'order')) {
                $table->integer('order')->default(0)->after('answer_km');
            }
            if (! Schema::hasColumn('faqs', 'status')) {
                $table->boolean('status')->default(true)->after('order');
            }
        });
    }

    public function down(): void
    {
        Schema::table('faqs', function (Blueprint $table) {
            $columns = ['status', 'order', 'answer_km', 'answer', 'question_km', 'question'];
            $existingColumns = array_values(array_filter($columns, fn ($column) => Schema::hasColumn('faqs', $column)));

            if (count($existingColumns) > 0) {
                $table->dropColumn($existingColumns);
            }
        });
    }
};
