-- =====================================================================
-- Stock is taken when an order is confirmed + the "returned" status
-- (App\Utility\OrderStockUtility)
--
-- No schema change.
--
-- 1. business_settings.stock_on_confirm_since = the moment this file runs.
--    Orders placed from then on take their stock when they are confirmed
--    (a pending order reserves nothing) and give it back when they go back to
--    pending, are cancelled, or are returned. Older orders already took their
--    stock when they were placed and keep the old rule. Without this row the
--    new rule is off. Re-running never moves the moment.
-- 2. Arabic UI strings.
--
-- Same rows as migration 2026_09_30_000003_stock_on_confirm_and_returned_status.
-- Re-running the file changes nothing (only missing rows are inserted).
-- After running: php artisan cache:clear (settings and translations are cached).
-- =====================================================================

SET NAMES utf8mb4;

INSERT INTO `business_settings` (`type`, `value`, `created_at`, `updated_at`)
SELECT 'stock_on_confirm_since', UNIX_TIMESTAMP(), NOW(), NOW()
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM `business_settings` WHERE `type` = 'stock_on_confirm_since'
);

INSERT INTO `translations` (`lang`, `lang_key`, `lang_value`, `created_at`, `updated_at`)
SELECT s.lang, s.lang_key, s.lang_value, NOW(), NOW()
FROM (
        SELECT 'en' AS lang, 'returned' AS lang_key, 'Returned' AS lang_value
        UNION ALL SELECT 'ar', 'returned', 'مرتجع'
        UNION ALL SELECT 'sa', 'returned', 'مرتجع'
        UNION ALL SELECT 'en', 'return_products', 'Return Products'
        UNION ALL SELECT 'ar', 'return_products', 'إرجاع منتجات'
        UNION ALL SELECT 'sa', 'return_products', 'إرجاع منتجات'
        UNION ALL SELECT 'en', 'return_reason', 'Return Reason'
        UNION ALL SELECT 'ar', 'return_reason', 'سبب الإرجاع'
        UNION ALL SELECT 'sa', 'return_reason', 'سبب الإرجاع'
        UNION ALL SELECT 'en', 'return_notes', 'Return Notes'
        UNION ALL SELECT 'ar', 'return_notes', 'ملاحظات الإرجاع'
        UNION ALL SELECT 'sa', 'return_notes', 'ملاحظات الإرجاع'
        UNION ALL SELECT 'en', 'returned_at', 'Returned At'
        UNION ALL SELECT 'ar', 'returned_at', 'تاريخ الإرجاع'
        UNION ALL SELECT 'sa', 'returned_at', 'تاريخ الإرجاع'
        UNION ALL SELECT 'en', 'returned_products', 'Returned products'
        UNION ALL SELECT 'ar', 'returned_products', 'المنتجات المرتجعة'
        UNION ALL SELECT 'sa', 'returned_products', 'المنتجات المرتجعة'
        UNION ALL SELECT 'en', 'do_not_return', 'Do not return'
        UNION ALL SELECT 'ar', 'do_not_return', 'تراجع'
        UNION ALL SELECT 'sa', 'do_not_return', 'تراجع'
        UNION ALL SELECT 'en', 'which_products_were_returned', 'Which products were returned?'
        UNION ALL SELECT 'ar', 'which_products_were_returned', 'ما المنتجات التي رجعت؟'
        UNION ALL SELECT 'sa', 'which_products_were_returned', 'ما المنتجات التي رجعت؟'
        UNION ALL SELECT 'en', 'the_quantity_of_the_selected_products_goes_back_to_stock', 'The quantity of the selected products goes back to stock.'
        UNION ALL SELECT 'ar', 'the_quantity_of_the_selected_products_goes_back_to_stock', 'تعود كمية المنتجات المحددة إلى المخزون.'
        UNION ALL SELECT 'sa', 'the_quantity_of_the_selected_products_goes_back_to_stock', 'تعود كمية المنتجات المحددة إلى المخزون.'
        UNION ALL SELECT 'en', 'their_quantity_is_back_in_stock', 'their quantity is back in stock'
        UNION ALL SELECT 'ar', 'their_quantity_is_back_in_stock', 'وعادت كمياتها إلى المخزون'
        UNION ALL SELECT 'sa', 'their_quantity_is_back_in_stock', 'وعادت كمياتها إلى المخزون'
        UNION ALL SELECT 'en', 'none_of_the_selected_products_can_be_returned', 'None of the selected products can be returned'
        UNION ALL SELECT 'ar', 'none_of_the_selected_products_can_be_returned', 'لا يمكن إرجاع أي من المنتجات المحددة'
        UNION ALL SELECT 'sa', 'none_of_the_selected_products_can_be_returned', 'لا يمكن إرجاع أي من المنتجات المحددة'
        UNION ALL SELECT 'en', 'use_the_return_products_button_to_return_an_order', 'Use the Return Products button to return an order'
        UNION ALL SELECT 'ar', 'use_the_return_products_button_to_return_an_order', 'استخدم زر «إرجاع منتجات» لإرجاع الطلب'
        UNION ALL SELECT 'sa', 'use_the_return_products_button_to_return_an_order', 'استخدم زر «إرجاع منتجات» لإرجاع الطلب'
        UNION ALL SELECT 'en', 'the_order_is_returned', 'The order is returned'
        UNION ALL SELECT 'ar', 'the_order_is_returned', 'الطلب مرتجع'
        UNION ALL SELECT 'sa', 'the_order_is_returned', 'الطلب مرتجع'
        UNION ALL SELECT 'en', 'returned_order__not_included_in_the_total', 'Returned order - not included in the total'
        UNION ALL SELECT 'ar', 'returned_order__not_included_in_the_total', 'طلب مرتجع - غير محتسب في الإجمالي'
        UNION ALL SELECT 'sa', 'returned_order__not_included_in_the_total', 'طلب مرتجع - غير محتسب في الإجمالي'
        UNION ALL SELECT 'en', 'not_enough_stock_for_this_order', 'Not enough stock for this order'
        UNION ALL SELECT 'ar', 'not_enough_stock_for_this_order', 'المخزون لا يكفي لهذا الطلب'
        UNION ALL SELECT 'sa', 'not_enough_stock_for_this_order', 'المخزون لا يكفي لهذا الطلب'
        UNION ALL SELECT 'en', 'available', 'Available'
        UNION ALL SELECT 'ar', 'available', 'المتوفر'
        UNION ALL SELECT 'sa', 'available', 'المتوفر'
        UNION ALL SELECT 'en', 'requested', 'Requested'
        UNION ALL SELECT 'ar', 'requested', 'المطلوب'
        UNION ALL SELECT 'sa', 'requested', 'المطلوب'
        UNION ALL SELECT 'en', 'customer_refused_to_receive', 'Customer refused to receive'
        UNION ALL SELECT 'ar', 'customer_refused_to_receive', 'رفض الزبون الاستلام'
        UNION ALL SELECT 'sa', 'customer_refused_to_receive', 'رفض الزبون الاستلام'
        UNION ALL SELECT 'en', 'customer_could_not_be_reached', 'Customer could not be reached'
        UNION ALL SELECT 'ar', 'customer_could_not_be_reached', 'تعذّر الوصول إلى الزبون'
        UNION ALL SELECT 'sa', 'customer_could_not_be_reached', 'تعذّر الوصول إلى الزبون'
        UNION ALL SELECT 'en', 'damaged_product', 'Damaged product'
        UNION ALL SELECT 'ar', 'damaged_product', 'منتج تالف'
        UNION ALL SELECT 'sa', 'damaged_product', 'منتج تالف'
        UNION ALL SELECT 'en', 'wrong_product_sent', 'Wrong product sent'
        UNION ALL SELECT 'ar', 'wrong_product_sent', 'أُرسل منتج خاطئ'
        UNION ALL SELECT 'sa', 'wrong_product_sent', 'أُرسل منتج خاطئ'
        UNION ALL SELECT 'en', 'not_as_described', 'Not as described'
        UNION ALL SELECT 'ar', 'not_as_described', 'غير مطابق للوصف'
        UNION ALL SELECT 'sa', 'not_as_described', 'غير مطابق للوصف'
        UNION ALL SELECT 'en', 'customer_changed_their_mind', 'Customer changed their mind'
        UNION ALL SELECT 'ar', 'customer_changed_their_mind', 'الزبون غيّر رأيه'
        UNION ALL SELECT 'sa', 'customer_changed_their_mind', 'الزبون غيّر رأيه'
        UNION ALL SELECT 'en', 'other', 'Other'
        UNION ALL SELECT 'ar', 'other', 'سبب آخر'
        UNION ALL SELECT 'sa', 'other', 'سبب آخر'
) AS s
WHERE NOT EXISTS (
    SELECT 1 FROM `translations` t WHERE t.lang = s.lang AND t.lang_key = s.lang_key
);
