-- Add ALL missing Arabic translations for the dashboard
-- This script inserts/updates translations for keys used across all dashboard pages

DO $$
DECLARE
    _lang_code text := 'ar';
BEGIN

    -- First, ensure all translation keys exist
    INSERT INTO translation_keys (key, category, description, default_value) VALUES
        -- Agent keys
        ('agent.active', 'agent', 'Agent active status', 'Active'),
        ('agent.addFaq', 'agent', 'Add FAQ button', 'Add FAQ'),
        ('agent.answer', 'agent', 'FAQ answer', 'Answer'),
        ('agent.confirmDelete', 'agent', 'Confirm delete message', 'Click delete again to confirm'),
        ('agent.createAgent', 'agent', 'Create agent button', 'Create Agent'),
        ('agent.createFirstAgent', 'agent', 'Create first agent message', 'Create your first AI agent to get started'),
        ('agent.createToPreview', 'agent', 'Create to preview message', 'Create an agent to see a preview'),
        ('agent.deleted', 'agent', 'Agent deleted message', 'Agent deleted'),
        ('agent.deployAgent', 'agent', 'Deploy agent button', 'Deploy'),
        ('agent.draft', 'agent', 'Draft status', 'Draft'),
        ('agent.escalationPrompt', 'agent', 'Escalation prompt label', 'Escalation Prompt'),
        ('agent.failedToDelete', 'agent', 'Failed to delete error', 'Failed to delete'),
        ('agent.failedToLoad', 'agent', 'Failed to load error', 'Failed to load agents'),
        ('agent.failedToToggle', 'agent', 'Failed to toggle error', 'Failed to toggle'),
        ('agent.fallbackMessage', 'agent', 'Fallback message label', 'Fallback Message'),
        ('agent.greetingMessage', 'agent', 'Greeting message label', 'Greeting Message'),
        ('agent.language', 'agent', 'Language label', 'Language'),
        ('agent.maxRetries', 'agent', 'Max retries label', 'Max Retries'),
        ('agent.name', 'agent', 'Agent name label', 'Name'),
        ('agent.newAgent', 'agent', 'New agent button', 'New Agent'),
        ('agent.noAgentsMatch', 'agent', 'No agents match message', 'No agents match your search'),
        ('agent.noAgentsYet', 'agent', 'No agents yet message', 'No agents yet'),
        ('agent.paused', 'agent', 'Paused status', 'Paused'),
        ('agent.personality', 'agent', 'Personality label', 'Personality'),
        ('agent.question', 'agent', 'FAQ question', 'Question'),
        ('agent.searchPlaceholder', 'agent', 'Search placeholder', 'Search agents...'),

        -- Table booking keys
        ('tableBooking.loadFailed', 'tableBooking', 'Load failed error', 'Failed to load timeline'),
        ('tableBooking.timeline.title', 'tableBooking', 'Timeline title', 'Floor timeline'),
        ('tableBooking.timeline.prevDay', 'tableBooking', 'Previous day aria', 'Previous day'),
        ('tableBooking.timeline.nextDay', 'tableBooking', 'Next day aria', 'Next day'),
        ('tableBooking.timeline.day', 'tableBooking', 'Day view', 'Day'),
        ('tableBooking.timeline.week', 'tableBooking', 'Week view', 'Week'),
        ('tableBooking.timeline.unassigned', 'tableBooking', 'Unassigned bookings', '{count} booking(s) without a table — drag onto the grid to assign.'),
        ('tableBooking.timeline.noTables', 'tableBooking', 'No tables message', 'No active tables. Add tables in the Tables section first.'),
        ('tableBooking.newBooking', 'tableBooking', 'New booking title', 'New booking'),
        ('tableBooking.tableLabel', 'tableBooking', 'Table label', 'Table'),
        ('tableBooking.guestName', 'tableBooking', 'Guest name label', 'Guest name'),
        ('tableBooking.guestNamePlaceholder', 'tableBooking', 'Guest name placeholder', 'e.g. Walk-in / Smith'),
        ('tableBooking.guestRequired', 'tableBooking', 'Guest required error', 'Guest name is required'),
        ('tableBooking.partySize', 'tableBooking', 'Party size label', 'Party size'),
        ('tableBooking.duration', 'tableBooking', 'Duration label', 'Duration (min)'),
        ('tableBooking.createBooking', 'tableBooking', 'Create booking button', 'Create booking'),
        ('tableBooking.walkInCreated', 'tableBooking', 'Walk-in created message', 'Booking created'),

        -- Storefront keys
        ('storefront.editor.title', 'storefront', 'Storefront editor title', 'QR Storefront'),
        ('storefront.editor.subtitle', 'storefront', 'Storefront editor subtitle', 'Design the QR and storefront for this branch'),
        ('storefront.editor.slugInvalid', 'storefront', 'Slug invalid error', 'Slug must be lowercase letters, digits and hyphens (e.g. downtown).'),
        ('storefront.editor.saved', 'storefront', 'Saved message', 'Storefront saved'),
        ('storefront.editor.savePublishFirst', 'storefront', 'Save first error', 'Save the storefront with a valid slug first.'),
        ('storefront.editor.pdfFailed', 'storefront', 'PDF failed error', 'Failed to generate PDF'),
        ('storefront.editor.notFound', 'storefront', 'Not found error', 'Branch not found'),
        ('storefront.editor.backToList', 'storefront', 'Back to list button', 'Back to QR Storefront'),
        ('storefront.editor.openPublic', 'storefront', 'Open public storefront button', 'Open public storefront'),
        ('storefront.noTables', 'storefront', 'No tables message', 'No tables yet'),
        ('storefront.noTablesDesc', 'storefront', 'No tables description', 'Create tables to generate QR codes for customers'),
        ('storefront.qrDesign', 'storefront', 'QR design label', 'QR design'),
        ('storefront.qrDesignDesc', 'storefront', 'QR design description', 'This style is used for every table QR card and the printed PDF. Changes preview live.'),
        ('storefront.qrCode', 'storefront', 'QR code label', 'QR code'),
        ('storefront.branchStorefront', 'storefront', 'Branch storefront label', 'Branch storefront'),
        ('storefront.foreground', 'storefront', 'Foreground label', 'Foreground'),
        ('storefront.background', 'storefront', 'Background label', 'Background'),
        ('storefront.margin', 'storefront', 'Margin label', 'Margin'),
        ('storefront.errorCorrection', 'storefront', 'Error correction label', 'Error correction'),
        ('storefront.errorCorrection.medium', 'storefront', 'Medium error correction', 'Medium'),
        ('storefront.printFrame', 'storefront', 'Print frame label', 'Print frame'),
        ('storefront.noBorder', 'storefront', 'No border option', 'No border'),
        ('storefront.centerLogo', 'storefront', 'Center logo option', 'Center logo (optional)'),
        ('storefront.uploadLogo', 'storefront', 'Upload logo button', 'Upload logo'),
        ('storefront.settings', 'storefront', 'Settings label', 'Storefront settings'),
        ('storefront.branchSlug', 'storefront', 'Branch slug label', 'Branch slug'),
        ('storefront.branchSlugDesc', 'storefront', 'Branch slug description', 'Used in the public URL — lowercase letters, digits, hyphens.'),
        ('storefront.enabled', 'storefront', 'Enabled label', 'Storefront enabled'),
        ('storefront.acceptDineIn', 'storefront', 'Accept dine-in label', 'Accept dine-in (QR table)'),
        ('storefront.acceptTakeaway', 'storefront', 'Accept takeaway label', 'Accept takeaway / pickup'),
        ('storefront.acceptDelivery', 'storefront', 'Accept delivery label', 'Accept delivery'),
        ('storefront.minOrder', 'storefront', 'Minimum order label', 'Minimum order (USD)'),
        ('storefront.bannerMessage', 'storefront', 'Banner message label', 'Storefront banner message'),
        ('storefront.bannerMessageDesc', 'storefront', 'Banner message description', 'Holiday hours, special promo, etc.'),
        ('storefront.payInPerson', 'storefront', 'Pay in person label', 'Storefront orders are pay-in-person (cash on delivery / pay at counter).'),
        ('storefront.save', 'storefront', 'Save button', 'Save storefront & QR'),

        -- Restaurant Settings keys
        ('restaurantSettings.billing.usage.conversations', 'settings', 'Conversations usage', 'AI Conversations'),
        ('restaurantSettings.billing.usage.voiceMinutes', 'settings', 'Voice minutes usage', 'Voice Minutes'),
        ('restaurantSettings.billing.usage.branches', 'settings', 'Branches usage', 'Branches'),
        ('restaurantSettings.billing.usage.apiCalls', 'settings', 'API calls usage', 'API Calls'),
        ('restaurantSettings.billing.usage.units.conversations', 'settings', 'Conversations unit', 'conversations'),
        ('restaurantSettings.billing.usage.units.voiceMinutes', 'settings', 'Voice minutes unit', 'minutes'),
        ('restaurantSettings.billing.usage.units.branches', 'settings', 'Branches unit', 'branches'),
        ('restaurantSettings.billing.usage.units.apiCalls', 'settings', 'API calls unit', 'calls'),
        ('restaurantSettings.templates.vars.customerName', 'settings', 'Customer name variable', 'Customer Name'),
        ('restaurantSettings.templates.vars.orderId', 'settings', 'Order ID variable', 'Order ID'),
        ('restaurantSettings.templates.vars.orderItems', 'settings', 'Order items variable', 'Order Items'),
        ('restaurantSettings.templates.vars.orderTotal', 'settings', 'Order total variable', 'Order Total'),
        ('restaurantSettings.templates.vars.estimatedTime', 'settings', 'Estimated time variable', 'Estimated Time'),
        ('restaurantSettings.templates.vars.restaurantName', 'settings', 'Restaurant name variable', 'Store Name'),
        ('restaurantSettings.templates.vars.trackingLink', 'settings', 'Tracking link variable', 'Tracking Link'),
        ('restaurantSettings.templates.vars.bookingId', 'settings', 'Booking ID variable', 'Booking ID'),
        ('restaurantSettings.templates.vars.bookingDate', 'settings', 'Booking date variable', 'Booking Date'),
        ('restaurantSettings.templates.vars.bookingTime', 'settings', 'Booking time variable', 'Booking Time'),
        ('restaurantSettings.templates.vars.partySize', 'settings', 'Party size variable', 'Party Size'),
        ('restaurantSettings.templates.vars.tableNumber', 'settings', 'Table number variable', 'Table Number'),
        ('restaurantSettings.templates.vars.restaurantAddress', 'settings', 'Restaurant address variable', 'Store Address'),
        ('restaurantSettings.templates.vars.cancelLink', 'settings', 'Cancel link variable', 'Cancel Link'),
        ('restaurantSettings.templates.vars.alertType', 'settings', 'Alert type variable', 'Alert Type'),
        ('restaurantSettings.templates.vars.alertMessage', 'settings', 'Alert message variable', 'Alert Message'),
        ('restaurantSettings.templates.vars.supportLink', 'settings', 'Support link variable', 'Support Link'),
        ('restaurantSettings.templates.orderConfirmation.label', 'settings', 'Order confirmation template', 'Order Confirmation'),
        ('restaurantSettings.templates.orderConfirmation.description', 'settings', 'Order confirmation description', 'Sent when a customer order is successfully placed.'),
        ('restaurantSettings.templates.bookingConfirmation.label', 'settings', 'Booking confirmation template', 'Booking Confirmation'),
        ('restaurantSettings.templates.bookingConfirmation.description', 'settings', 'Booking confirmation description', 'Sent when a table reservation is confirmed.'),
        ('restaurantSettings.templates.customerAlert.label', 'settings', 'Customer alert template', 'Customer Alert'),
        ('restaurantSettings.templates.customerAlert.description', 'settings', 'Customer alert description', 'Sent to notify customers about delays or updates.'),

        -- REST API keys
        ('restApi.endpoints.listOrders', 'settings', 'List orders endpoint', 'List all orders with filters and pagination'),
        ('restApi.endpoints.getOrder', 'settings', 'Get order endpoint', 'Get a single order by ID'),
        ('restApi.endpoints.createOrder', 'settings', 'Create order endpoint', 'Create a new order'),
        ('restApi.endpoints.updateOrder', 'settings', 'Update order endpoint', 'Update order status or details'),
        ('restApi.endpoints.listBookings', 'settings', 'List bookings endpoint', 'List all table bookings'),
        ('restApi.endpoints.createBooking', 'settings', 'Create booking endpoint', 'Create a new table booking'),
        ('restApi.endpoints.cancelBooking', 'settings', 'Cancel booking endpoint', 'Cancel a booking'),
        ('restApi.endpoints.listCustomers', 'settings', 'List customers endpoint', 'List all customers'),
        ('restApi.endpoints.getCustomer', 'settings', 'Get customer endpoint', 'Get customer profile and order history'),
        ('restApi.endpoints.listMenuItems', 'settings', 'List menu items endpoint', 'List all menu items'),
        ('restApi.endpoints.createMenuItem', 'settings', 'Create menu item endpoint', 'Create a new menu item'),
        ('restApi.endpoints.analyticsSummary', 'settings', 'Analytics summary endpoint', 'Get revenue and order analytics summary'),
        ('restApi.endpoints.listConversations', 'settings', 'List conversations endpoint', 'List AI conversations'),
        ('restApi.endpoints.takeoverConversation', 'settings', 'Takeover conversation endpoint', 'Take over an AI conversation'),

        -- Marketplace/Integration keys
        ('marketplace.square.name', 'marketplace', 'Square integration name', 'Square POS'),
        ('marketplace.square.description', 'marketplace', 'Square integration description', 'Sync orders, menu items, and inventory directly from your Square point-of-sale system.'),
        ('marketplace.square.step1.title', 'marketplace', 'Square step 1', 'Create a Square Developer Account'),
        ('marketplace.square.step1.desc', 'marketplace', 'Square step 1 desc', 'Go to developer.squareup.com and sign in with your Square account.'),
        ('marketplace.square.step2.title', 'marketplace', 'Square step 2', 'Generate API Credentials'),
        ('marketplace.square.step2.desc', 'marketplace', 'Square step 2 desc', 'Create a new application and copy your Application ID and Access Token.'),
        ('marketplace.square.step3.title', 'marketplace', 'Square step 3', 'Enter Credentials in RestroAgent'),
        ('marketplace.square.step3.desc', 'marketplace', 'Square step 3 desc', 'Paste your Application ID and Access Token and click Authorize.'),
        ('marketplace.square.step4.title', 'marketplace', 'Square step 4', 'Select Location'),
        ('marketplace.square.step4.desc', 'marketplace', 'Square step 4 desc', 'Choose which Square location to sync with this RestroAgent branch.'),
        ('marketplace.square.step5.title', 'marketplace', 'Square step 5', 'Configure Sync Settings'),
        ('marketplace.square.step5.desc', 'marketplace', 'Square step 5 desc', 'Choose what data to sync: menu items, inventory levels, orders, and customer data.'),

        -- Common keys that might be missing
        ('common.loadFailed', 'common', 'Load failed', 'Failed to load'),
        ('common.saveFailed', 'common', 'Save failed', 'Save failed'),
        ('common.loading', 'common', 'Loading', 'Loading…'),
        ('common.saving', 'common', 'Saving', 'Saving…'),
        ('common.savingDots', 'common', 'Saving with dots', 'Saving…'),
        ('common.today', 'common', 'Today', 'Today'),
        ('common.close', 'common', 'Close', 'Close'),
        ('common.cancel', 'common', 'Cancel', 'Cancel'),
        ('common.save', 'common', 'Save', 'Save'),
        ('common.tryDifferentSearch', 'common', 'Try different search', 'Try a different search term'),
        ('common.days.monday', 'common', 'Monday', 'Monday'),
        ('common.days.tuesday', 'common', 'Tuesday', 'Tuesday'),
        ('common.days.wednesday', 'common', 'Wednesday', 'Wednesday'),
        ('common.days.thursday', 'common', 'Thursday', 'Thursday'),
        ('common.days.friday', 'common', 'Friday', 'Friday'),
        ('common.days.saturday', 'common', 'Saturday', 'Saturday'),
        ('common.days.sunday', 'common', 'Sunday', 'Sunday'),
        ('common.sms', 'common', 'SMS', 'SMS'),
        ('common.whatsapp', 'common', 'WhatsApp', 'WhatsApp'),
        ('common.email', 'common', 'Email', 'Email'),
        ('common.date', 'common', 'Date', 'Date'),
        ('common.time', 'common', 'Time', 'Time'),
        ('common.total', 'common', 'Total', 'Total'),
        ('common.status', 'common', 'Status', 'Status'),
        ('common.notes', 'common', 'Notes', 'Notes'),
        ('common.viewAll', 'common', 'View All', 'View All'),
        ('common.of', 'common', 'Of', 'of'),
        ('common.page', 'common', 'Page', 'Page'),
        ('common.prev', 'common', 'Previous', 'Prev'),
        ('common.next', 'common', 'Next', 'Next'),
        ('common.refresh', 'common', 'Refresh', 'Refresh'),
        ('common.phone', 'common', 'Phone', 'Phone')
    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
            -- Agent translations
            ('agent.active', 'نشط'),
            ('agent.addFaq', 'إضافة سؤال شائع'),
            ('agent.answer', 'الإجابة'),
            ('agent.confirmDelete', 'انقر حذف مرة أخرى للتأكيد'),
            ('agent.createAgent', 'إنشاء وكيل'),
            ('agent.createFirstAgent', 'أنشئ أول وكيل ذكاء اصطناعي للبدء'),
            ('agent.createToPreview', 'أنشئ وكيلًا لرؤية معاينة'),
            ('agent.deleted', 'تم حذف الوكيل'),
            ('agent.deployAgent', 'نشر'),
            ('agent.draft', 'مسودة'),
            ('agent.escalationPrompt', 'مطالبة التصعيد'),
            ('agent.failedToDelete', 'فشل في الحذف'),
            ('agent.failedToLoad', 'فشل في تحميل الوكلاء'),
            ('agent.failedToToggle', 'فشل في التبديل'),
            ('agent.fallbackMessage', 'رسالة الاحتياطي'),
            ('agent.greetingMessage', 'رسالة الترحيب'),
            ('agent.language', 'اللغة'),
            ('agent.maxRetries', 'الحد الأقصى للمحاولات'),
            ('agent.name', 'الاسم'),
            ('agent.newAgent', 'وكيل جديد'),
            ('agent.noAgentsMatch', 'لا توجد وكلاء تطابق بحثك'),
            ('agent.noAgentsYet', 'لا توجد وكلاء بعد'),
            ('agent.paused', 'متوقف'),
            ('agent.personality', 'الشخصية'),
            ('agent.question', 'السؤال'),
            ('agent.searchPlaceholder', 'البحث في الوكلاء...'),

            -- Table booking translations
            ('tableBooking.loadFailed', 'فشل في تحميل الجدول الزمني'),
            ('tableBooking.timeline.title', 'الجدول الزمني للطابق'),
            ('tableBooking.timeline.prevDay', 'اليوم السابق'),
            ('tableBooking.timeline.nextDay', 'اليوم التالي'),
            ('tableBooking.timeline.day', 'يوم'),
            ('tableBooking.timeline.week', 'أسبوع'),
            ('tableBooking.timeline.unassigned', '{count} حجز(حجوزات) بدون طاولة — اسحب إلى الشبكة للتعيين.'),
            ('tableBooking.timeline.noTables', 'لا توجد طاولات نشطة. أضف طاولات في قسم الطاولات أولاً.'),
            ('tableBooking.newBooking', 'حجز جديد'),
            ('tableBooking.tableLabel', 'الطاولة'),
            ('tableBooking.guestName', 'اسم الضيف'),
            ('tableBooking.guestNamePlaceholder', 'مثال: زيارة / سميث'),
            ('tableBooking.guestRequired', 'اسم الضيف مطلوب'),
            ('tableBooking.partySize', 'حجم المجموعة'),
            ('tableBooking.duration', 'المدة (دقيقة)'),
            ('tableBooking.createBooking', 'إنشاء حجز'),
            ('tableBooking.walkInCreated', 'تم إنشاء الحجز'),

            -- Storefront translations
            ('storefront.editor.title', 'واجهة المتجر بالرمز'),
            ('storefront.editor.subtitle', 'تصميم الرمز وواجهة المتجر لهذا الفرع'),
            ('storefront.editor.slugInvalid', 'يجب أن يكون الرابط أحرف صغيرة وأرقام وشرطات (مثال: downtown).'),
            ('storefront.editor.saved', 'تم حفظ واجهة المتجر'),
            ('storefront.editor.savePublishFirst', 'احفظ واجهة المتجر برابط صالح أولاً.'),
            ('storefront.editor.pdfFailed', 'فشل في إنشاء PDF'),
            ('storefront.editor.notFound', 'الفرع غير موجود'),
            ('storefront.editor.backToList', 'العودة إلى واجهة المتجر بالرمز'),
            ('storefront.editor.openPublic', 'فتح واجهة المتجر العامة'),
            ('storefront.noTables', 'لا توجد طاولات بعد'),
            ('storefront.noTablesDesc', 'أنشئ طاولات لإنشاء رموز QR للعملاء'),
            ('storefront.qrDesign', 'تصميم الرمز'),
            ('storefront.qrDesignDesc', 'يُستخدم هذا النمط لكل بطاقة رمز طاولة وملف PDF المطبوع. يتغير المعاينة مباشرة.'),
            ('storefront.qrCode', 'رمز الاستجابة السريعة'),
            ('storefront.branchStorefront', 'واجهة المتجر للفرع'),
            ('storefront.foreground', 'المقدمة'),
            ('storefront.background', 'الخلفية'),
            ('storefront.margin', 'الهامش'),
            ('storefront.errorCorrection', 'تصحيح الأخطاء'),
            ('storefront.errorCorrection.medium', 'متوسط'),
            ('storefront.printFrame', 'إطار الطباعة'),
            ('storefront.noBorder', 'بدون إطار'),
            ('storefront.centerLogo', 'شعار في الوسط (اختياري)'),
            ('storefront.uploadLogo', 'رفع الشعار'),
            ('storefront.settings', 'إعدادات واجهة المتجر'),
            ('storefront.branchSlug', 'رابط الفرع'),
            ('storefront.branchSlugDesc', 'يُستخدم في الرابط العام — أحرف صغيرة وأرقام وشرطات.'),
            ('storefront.enabled', 'واجهة المتجر مفعلة'),
            ('storefront.acceptDineIn', 'قبول الطعام في المكان (رمز الطاولة)'),
            ('storefront.acceptTakeaway', 'قبول الاستلام'),
            ('storefront.acceptDelivery', 'قبول التوصيل'),
            ('storefront.minOrder', 'الحد الأدنى للطلب (USD)'),
            ('storefront.bannerMessage', 'رسالة لافتة واجهة المتجر'),
            ('storefront.bannerMessageDesc', 'ساعات العطلة، عروض خاصة، إلخ.'),
            ('storefront.payInPerson', 'طلبات واجهة المتجر دفع عند الاستلام (نقداً عند التوصيل / الدفع في الطاولة).'),
            ('storefront.save', 'حفظ واجهة المتجر والرمز'),

            -- Restaurant Settings translations
            ('restaurantSettings.billing.usage.conversations', 'محادثات الذكاء الاصطناعي'),
            ('restaurantSettings.billing.usage.voiceMinutes', 'دقائق الصوت'),
            ('restaurantSettings.billing.usage.branches', 'الفروع'),
            ('restaurantSettings.billing.usage.apiCalls', 'استدعاءات API'),
            ('restaurantSettings.billing.usage.units.conversations', 'محادثات'),
            ('restaurantSettings.billing.usage.units.voiceMinutes', 'دقائق'),
            ('restaurantSettings.billing.usage.units.branches', 'فروع'),
            ('restaurantSettings.billing.usage.units.apiCalls', 'استدعاء'),
            ('restaurantSettings.templates.vars.customerName', 'اسم العميل'),
            ('restaurantSettings.templates.vars.orderId', 'رقم الطلب'),
            ('restaurantSettings.templates.vars.orderItems', 'عناصر الطلب'),
            ('restaurantSettings.templates.vars.orderTotal', 'إجمالي الطلب'),
            ('restaurantSettings.templates.vars.estimatedTime', 'الوقت المقدر'),
            ('restaurantSettings.templates.vars.restaurantName', 'اسم المتجر'),
            ('restaurantSettings.templates.vars.trackingLink', 'رابط التتبع'),
            ('restaurantSettings.templates.vars.bookingId', 'رقم الحجز'),
            ('restaurantSettings.templates.vars.bookingDate', 'تاريخ الحجز'),
            ('restaurantSettings.templates.vars.bookingTime', 'وقت الحجز'),
            ('restaurantSettings.templates.vars.partySize', 'حجم المجموعة'),
            ('restaurantSettings.templates.vars.tableNumber', 'رقم الطاولة'),
            ('restaurantSettings.templates.vars.restaurantAddress', 'عنوان المتجر'),
            ('restaurantSettings.templates.vars.cancelLink', 'رابط الإلغاء'),
            ('restaurantSettings.templates.vars.alertType', 'نوع التنبيه'),
            ('restaurantSettings.templates.vars.alertMessage', 'رسالة التنبيه'),
            ('restaurantSettings.templates.vars.supportLink', 'رابط الدعم'),
            ('restaurantSettings.templates.orderConfirmation.label', 'تأكيد الطلب'),
            ('restaurantSettings.templates.orderConfirmation.description', 'يُرسل عند تقديم طلب العميل بنجاح.'),
            ('restaurantSettings.templates.bookingConfirmation.label', 'تأكيد الحجز'),
            ('restaurantSettings.templates.bookingConfirmation.description', 'يُرسل عند تأكيد حجز طاولة.'),
            ('restaurantSettings.templates.customerAlert.label', 'تنبيه العميل'),
            ('restaurantSettings.templates.customerAlert.description', 'يُرسل لإبلاغ العملاء عن التأخيرات أو التحديثات.'),

            -- REST API translations
            ('restApi.endpoints.listOrders', 'قائمة جميع الطلبات مع الفلاتر والترقيم'),
            ('restApi.endpoints.getOrder', 'الحصول على طلب واحد حسب المعرف'),
            ('restApi.endpoints.createOrder', 'إنشاء طلب جديد'),
            ('restApi.endpoints.updateOrder', 'تحديث حالة الطلب أو التفاصيل'),
            ('restApi.endpoints.listBookings', 'قائمة جميع حجوزات الطاولات'),
            ('restApi.endpoints.createBooking', 'إنشاء حجز طاولة جديد'),
            ('restApi.endpoints.cancelBooking', 'إلغاء حجز'),
            ('restApi.endpoints.listCustomers', 'قائمة جميع العملاء'),
            ('restApi.endpoints.getCustomer', 'الحصول على ملف العميل وسجل الطلبات'),
            ('restApi.endpoints.listMenuItems', 'قائمة جميع عناصر القائمة'),
            ('restApi.endpoints.createMenuItem', 'إنشاء عنصر قائمة جديد'),
            ('restApi.endpoints.analyticsSummary', 'الحصول على ملخص تحليلات الإيرادات والطلبات'),
            ('restApi.endpoints.listConversations', 'قائمة محادثات الذكاء الاصطناعي'),
            ('restApi.endpoints.takeoverConversation', 'السيطرة على محادثة الذكاء الاصطناعي'),

            -- Marketplace translations
            ('marketplace.square.name', 'نقطة بيع Square'),
            ('marketplace.square.description', 'مزامنة الطلبات وعناصر القائمة والمخزون مباشرة من نظام نقطة بيع Square.'),
            ('marketplace.square.step1.title', 'إنشاء حساب مطور Square'),
            ('marketplace.square.step1.desc', 'اذهب إلى developer.squareup.com وسجل الدخول بحساب Square الخاص بك.'),
            ('marketplace.square.step2.title', 'إنشاء بيانات اعتماد API'),
            ('marketplace.square.step2.desc', 'قم بإنشاء تطبيق جديد وانسخ معرف التطبيق ورمز الوصول.'),
            ('marketplace.square.step3.title', 'إدخال بيانات الاعتماد في RestroAgent'),
            ('marketplace.square.step3.desc', 'الصق معرف التطبيق ورمز الوصول وانقر فوق تفويض.'),
            ('marketplace.square.step4.title', 'اختيار الموقع'),
            ('marketplace.square.step4.desc', 'اختر موقع Square لمزامنته مع فرع RestroAgent هذا.'),
            ('marketplace.square.step5.title', 'تكوين إعدادات المزامنة'),
            ('marketplace.square.step5.desc', 'اختر البيانات المطلوب مزامنتها: عناصر القائمة ومستويات المخزون والطلبات وبيانات العملاء.'),

            -- Common translations
            ('common.loadFailed', 'فشل في التحميل'),
            ('common.saveFailed', 'فشل الحفظ'),
            ('common.loading', 'جارٍ التحميل...'),
            ('common.saving', 'جارٍ الحفظ...'),
            ('common.savingDots', 'جارٍ الحفظ...'),
            ('common.today', 'اليوم'),
            ('common.close', 'إغلاق'),
            ('common.cancel', 'إلغاء'),
            ('common.save', 'حفظ'),
            ('common.tryDifferentSearch', 'جرب مصطلح بحث مختلف'),
            ('common.days.monday', 'الاثنين'),
            ('common.days.tuesday', 'الثلاثاء'),
            ('common.days.wednesday', 'الأربعاء'),
            ('common.days.thursday', 'الخميس'),
            ('common.days.friday', 'الجمعة'),
            ('common.days.saturday', 'السبت'),
            ('common.days.sunday', 'الأحد'),
            ('common.sms', 'رسالة نصية'),
            ('common.whatsapp', 'واتساب'),
            ('common.email', 'البريد الإلكتروني'),
            ('common.date', 'التاريخ'),
            ('common.time', 'الوقت'),
            ('common.total', 'الإجمالي'),
            ('common.status', 'الحالة'),
            ('common.notes', 'الملاحظات'),
            ('common.viewAll', 'عرض الكل'),
            ('common.of', 'من'),
            ('common.page', 'الصفحة'),
            ('common.prev', 'السابق'),
            ('common.next', 'التالي'),
            ('common.refresh', 'تحديث'),
            ('common.phone', 'الهاتف')
    ) 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 'All missing Arabic translations added successfully';
END $$;
