<?php

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

/**
 * Add `language` to chat_widgets.
 *
 * Why: the live-chat widget's UI strings ("Online now", "Type a message…",
 * "Start a conversation", etc.) need to render in the workspace's chosen
 * language, not always English. Storing the locale on the same row that
 * /api/widget/status reads avoids an extra join on every visitor's page
 * load — the public endpoint is hit on EVERY page where the widget is
 * embedded, so query weight matters.
 *
 * `nullable` semantics: an empty value means "use the workspace's
 * default locale" (which itself falls back to the platform default).
 * That gives us a sane three-level fallback chain without forcing
 * existing customers to pick a language explicitly.
 */
return new class extends Migration {
    public function up(): void
    {
        if (Schema::hasTable('chat_widgets') && !Schema::hasColumn('chat_widgets', 'language')) {
            Schema::table('chat_widgets', function (Blueprint $t) {
                $t->string('language', 8)->nullable()->after('show_branding');
            });
        }
    }

    public function down(): void
    {
        if (Schema::hasTable('chat_widgets') && Schema::hasColumn('chat_widgets', 'language')) {
            Schema::table('chat_widgets', function (Blueprint $t) {
                $t->dropColumn('language');
            });
        }
    }
};
