-- =====================================================================
-- Kapitano Logistic integration - orders that must be wrapped before delivery
--
-- 1. orders.needs_wrapping (default 0): ticked by the customer at checkout or by staff
--    before the order is handed to Logistic.
-- 2. the screen texts, English and Arabic.
--
-- Safe to run more than once. Afterwards: php artisan cache:clear
-- =====================================================================

SET NAMES utf8mb4;

SET @has := (SELECT COUNT(*) FROM information_schema.COLUMNS
             WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'orders' AND COLUMN_NAME = 'needs_wrapping');
SET @ddl := IF(@has = 0, 'ALTER TABLE `orders` ADD COLUMN `needs_wrapping` TINYINT(1) NOT NULL DEFAULT 0', 'SELECT 1');
PREPARE stmt FROM @ddl;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- (key, English, Arabic)
INSERT INTO `translations` (`lang`, `lang_key`, `lang_value`, `created_at`, `updated_at`)
SELECT v.lang, v.k, v.val, NOW(), NOW() FROM (
  SELECT 'en' lang, 'wrap_this_order_before_delivery' k, 'Wrap this order before delivery' val
  UNION ALL SELECT 'ar', 'wrap_this_order_before_delivery', 'تغليف هذا الطلب قبل التوصيل'
  UNION ALL SELECT 'en', 'wrapping_can_only_be_changed_before_the_order_is_sent_to_kapitano_logistic', 'Wrapping can only be changed before the order is sent to Kapitano Logistic.'
  UNION ALL SELECT 'ar', 'wrapping_can_only_be_changed_before_the_order_is_sent_to_kapitano_logistic', 'لا يمكن تغيير التغليف إلا قبل إرسال الطلب إلى كابيتانو لوجستك.'
  UNION ALL SELECT 'en', 'wrapping_setting_saved', 'Wrapping setting saved'
  UNION ALL SELECT 'ar', 'wrapping_setting_saved', 'تم حفظ إعداد التغليف'
  UNION ALL SELECT 'en', 'needs_wrapping', 'Needs wrapping'
  UNION ALL SELECT 'ar', 'needs_wrapping', 'يحتاج تغليفًا'
  UNION ALL SELECT 'en', 'the_wrapping_place_is_set_by_the_store_in_its_logistic_portal_settings', 'The wrapping place is set by the store in its Logistic portal (Settings).'
  UNION ALL SELECT 'ar', 'the_wrapping_place_is_set_by_the_store_in_its_logistic_portal_settings', 'يحدد المتجر مكان التغليف من إعدادات بوابته في لوجستك.'
) v
WHERE NOT EXISTS (SELECT 1 FROM `translations` t WHERE t.lang = v.lang AND t.lang_key = v.k);
