-- Add missing Arabic translations for WhatsApp, integrations, and other terms
DO $$
DECLARE
    _lang_code text := 'ar';
BEGIN

    -- First, ensure all translation keys exist
    INSERT INTO translation_keys (key, category, description, default_value) VALUES
        ('admin.aiSettings.flags.whatsapp', 'admin', 'WhatsApp Integration', 'WhatsApp Integration'),
        ('admin.aiSettings.flags.whatsappDesc', 'admin', 'WhatsApp Integration description', 'Allow restaurants to connect WhatsApp Business'),
        ('admin.branding.appearance', 'admin', 'Appearance', 'Appearance'),
        ('admin.branding.appearanceDescription', 'admin', 'Appearance description', 'Primary brand color used across landing pages, buttons, and accents'),
        ('billing.features.whatsappAgent', 'billing', 'WhatsApp Agent', 'WhatsApp Agent'),
        ('billing.features.whatsappAgentDesc', 'billing', 'WhatsApp Agent description', 'WhatsApp Business inbound + outbound via Meta Cloud API'),
        ('channel.whatsapp', 'common', 'WhatsApp channel', 'WhatsApp'),
        ('common.approve', 'common', 'Approve', 'Approve'),
        ('landing.integrationsPage.badge', 'landing', 'Integrations badge', 'Ecosystem'),
        ('landing.integrationsPage.cta1Link', 'landing', 'CTA 1 link', 'Request an integration'),
        ('landing.integrationsPage.cta1Sub', 'landing', 'CTA 1 subtitle', 'We build new connectors at no extra cost. Tell us what you use and we'),
        ('landing.integrationsPage.cta1Title', 'landing', 'CTA 1 title', 'Don''t see your integration?'),
        ('landing.integrationsPage.cta2Link', 'landing', 'CTA 2 link', 'Read our security docs'),
        ('landing.integrationsPage.cta2Sub', 'landing', 'CTA 2 subtitle', 'OAuth, webhooks, retries, and idempotent syncs — built for production restaurants.'),
        ('landing.integrationsPage.cta2Title', 'landing', 'CTA 2 title', 'Enterprise-grade reliability'),
        ('landing.integrationsPage.sub', 'landing', 'Integrations subtitle', 'Native integrations with 50+ POS, delivery, telephony, messaging, and payment platforms — no rip-and-replace required.'),
        ('landing.integrationsPage.title', 'landing', 'Integrations title', 'Connects with your entire stack'),
        ('landing.int.whatsapp', 'landing', 'WhatsApp integration', 'WhatsApp'),
        ('landing.int.whatsappDesc', 'landing', 'WhatsApp description', 'Official WhatsApp Business API messaging.'),
        ('marketing.audience.channelWhatsapp', 'marketing', 'WhatsApp only', 'WhatsApp only')
    ON CONFLICT (key) DO NOTHING;

    -- Now insert/update all Arabic translations
    INSERT INTO translations (lang_code, key_id, value)
    SELECT
        _lang_code,
        tk.id,
        v.arabic_value
    FROM (
        VALUES
            ('admin.aiSettings.flags.whatsapp', 'تكامل واتساب'),
            ('admin.aiSettings.flags.whatsappDesc', 'السماح للمطاعم بالاتصال بواتساب للأعمال'),
            ('admin.branding.appearance', 'المظهر'),
            ('admin.branding.appearanceDescription', 'لون العلامة التجارية الأساسي المستخدم في صفحات الهبوط والأزرار والنقاط البارزة'),
            ('billing.features.whatsappAgent', 'وكيل واتساب'),
            ('billing.features.whatsappAgentDesc', 'واتساب للأعمال الوارد والصادر عبر واجهة برمجة التطبيقات السحابية من ميتا'),
            ('channel.whatsapp', 'واتساب'),
            ('common.approve', 'موافقة'),
            ('landing.integrationsPage.badge', 'النظام البيئي'),
            ('landing.integrationsPage.cta1Link', 'طلب تكامل'),
            ('landing.integrationsPage.cta1Sub', 'نحن نبني موصلات جديدة بتكلفة إضافية. أخبرنا بما تستخدم وسن'),
            ('landing.integrationsPage.cta1Title', 'لا ترى التكامل الخاص بك؟'),
            ('landing.integrationsPage.cta2Link', 'اقرأ وثائق الأمان الخاصة بنا'),
            ('landing.integrationsPage.cta2Sub', 'OAuth، خطافات ويب، إعادة المحاولة والمزامنة الثابتة — مصممة لمطاعم الإنتاج.'),
            ('landing.integrationsPage.cta2Title', 'موثوقية على مستوى المؤسسات'),
            ('landing.integrationsPage.sub', 'تكاملات أصلية مع أكثر من 50 نظام POS ومنصات توصيل والهاتف والمراسلة والدفع — لا حاجة للاستبدال الكامل.'),
            ('landing.integrationsPage.title', 'يتكامل مع نظامك بالكامل'),
            ('landing.int.whatsapp', 'واتساب'),
            ('landing.int.whatsappDesc', 'مراسلة عبر واجهة برمجة تطبيقات واتساب للأعمال الرسمية.'),
            ('marketing.audience.channelWhatsapp', 'واتساب فقط')
    ) AS v(key, arabic_value)
    JOIN translation_keys tk ON tk.key = v.key
    ON CONFLICT (lang_code, key_id) DO UPDATE SET value = EXCLUDED.value;

    RAISE NOTICE 'WhatsApp and integration Arabic translations added successfully';
END $$;
