-- =====================================================================
-- Traffic source analytics (Reports > Traffic Sources)
--
-- 1. Two NEW tables - no existing table is altered:
--      traffic_visits        one row per storefront visit
--      traffic_attributions  source of a new customer / of a checkout
-- 2. Permission `traffic_source_report` (Reports section), given to the
--    "full access" roles; tick it for other roles in Staff > Roles.
-- 3. Arabic UI strings.
--
-- Same statements as migration 2026_09_30_000010_create_traffic_source_tables.
-- Re-running the file changes nothing.
-- After running: php artisan cache:clear (tracking starts once the cached
-- "tables missing" flag is gone; permissions and translations are cached too).
-- =====================================================================

SET NAMES utf8mb4;

CREATE TABLE IF NOT EXISTS `traffic_visits` (
    `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    `visitor_token` CHAR(32) NOT NULL,
    `source` VARCHAR(20) NOT NULL,
    `referrer_host` VARCHAR(190) NULL,
    `utm_source` VARCHAR(100) NULL,
    `utm_medium` VARCHAR(100) NULL,
    `utm_campaign` VARCHAR(150) NULL,
    `landing_path` VARCHAR(255) NULL,
    `created_at` DATETIME NOT NULL,
    PRIMARY KEY (`id`),
    KEY `traffic_visits_created_source_index` (`created_at`, `source`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS `traffic_attributions` (
    `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    `subject_type` VARCHAR(20) NOT NULL,
    `subject_id` BIGINT UNSIGNED NOT NULL,
    `source` VARCHAR(20) NOT NULL,
    `visit_id` BIGINT UNSIGNED NULL,
    `created_at` DATETIME NOT NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `traffic_attributions_subject_unique` (`subject_type`, `subject_id`),
    KEY `traffic_attributions_type_created_index` (`subject_type`, `created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO `permissions` (`name`, `section`, `guard_name`, `created_at`, `updated_at`)
SELECT 'traffic_source_report', 'report', 'web', NOW(), NOW()
FROM DUAL
WHERE NOT EXISTS (
    SELECT 1 FROM `permissions` WHERE `name` = 'traffic_source_report' AND `guard_name` = 'web'
);

INSERT INTO `role_has_permissions` (`permission_id`, `role_id`)
SELECT p.id, r.id
FROM `permissions` p
JOIN `roles` r ON LOWER(r.name) = 'full access'
WHERE p.name = 'traffic_source_report' AND p.guard_name = 'web'
  AND NOT EXISTS (
    SELECT 1 FROM `role_has_permissions` x WHERE x.permission_id = p.id AND x.role_id = r.id
);

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, 'traffic_source_report' AS lang_key, 'Traffic Source Report' AS lang_value
        UNION ALL SELECT 'ar', 'traffic_source_report', 'تقرير مصادر الزيارات'
        UNION ALL SELECT 'sa', 'traffic_source_report', 'تقرير مصادر الزيارات'
        UNION ALL SELECT 'en', 'traffic_sources', 'Traffic Sources'
        UNION ALL SELECT 'ar', 'traffic_sources', 'مصادر الزيارات'
        UNION ALL SELECT 'sa', 'traffic_sources', 'مصادر الزيارات'
        UNION ALL SELECT 'en', 'where_visitors_new_customers_and_orders_come_from_counted_on_the_website_the_app_is_shown_as_its_own_source', 'Where visitors, new customers and orders come from. Counted on the website; the app is shown as its own source'
        UNION ALL SELECT 'ar', 'where_visitors_new_customers_and_orders_come_from_counted_on_the_website_the_app_is_shown_as_its_own_source', 'من أين يأتي الزوار والعملاء الجدد والطلبات. يُحتسب على الموقع، والتطبيق يظهر كمصدر مستقل'
        UNION ALL SELECT 'sa', 'where_visitors_new_customers_and_orders_come_from_counted_on_the_website_the_app_is_shown_as_its_own_source', 'من أين يأتي الزوار والعملاء الجدد والطلبات. يُحتسب على الموقع، والتطبيق يظهر كمصدر مستقل'
        UNION ALL SELECT 'en', 'facebook', 'Facebook'
        UNION ALL SELECT 'ar', 'facebook', 'فيسبوك'
        UNION ALL SELECT 'sa', 'facebook', 'فيسبوك'
        UNION ALL SELECT 'en', 'instagram', 'Instagram'
        UNION ALL SELECT 'ar', 'instagram', 'إنستغرام'
        UNION ALL SELECT 'sa', 'instagram', 'إنستغرام'
        UNION ALL SELECT 'en', 'google', 'Google'
        UNION ALL SELECT 'ar', 'google', 'غوغل'
        UNION ALL SELECT 'sa', 'google', 'غوغل'
        UNION ALL SELECT 'en', 'direct_traffic', 'Direct traffic'
        UNION ALL SELECT 'ar', 'direct_traffic', 'زيارات مباشرة'
        UNION ALL SELECT 'sa', 'direct_traffic', 'زيارات مباشرة'
        UNION ALL SELECT 'en', 'other_websites', 'Other websites'
        UNION ALL SELECT 'ar', 'other_websites', 'مواقع أخرى'
        UNION ALL SELECT 'sa', 'other_websites', 'مواقع أخرى'
        UNION ALL SELECT 'en', 'mobile_app', 'Mobile app'
        UNION ALL SELECT 'ar', 'mobile_app', 'تطبيق الجوال'
        UNION ALL SELECT 'sa', 'mobile_app', 'تطبيق الجوال'
        UNION ALL SELECT 'en', 'visits', 'Visits'
        UNION ALL SELECT 'ar', 'visits', 'الزيارات'
        UNION ALL SELECT 'sa', 'visits', 'الزيارات'
        UNION ALL SELECT 'en', 'unique_visitors', 'Unique Visitors'
        UNION ALL SELECT 'ar', 'unique_visitors', 'الزوار الفريدون'
        UNION ALL SELECT 'sa', 'unique_visitors', 'الزوار الفريدون'
        UNION ALL SELECT 'en', 'new_customers', 'New Customers'
        UNION ALL SELECT 'ar', 'new_customers', 'عملاء جدد'
        UNION ALL SELECT 'sa', 'new_customers', 'عملاء جدد'
        UNION ALL SELECT 'en', 'conversion_rate', 'Conversion Rate'
        UNION ALL SELECT 'ar', 'conversion_rate', 'نسبة التحويل'
        UNION ALL SELECT 'sa', 'conversion_rate', 'نسبة التحويل'
        UNION ALL SELECT 'en', 'orders_divided_by_visits', 'Orders divided by visits'
        UNION ALL SELECT 'ar', 'orders_divided_by_visits', 'عدد الطلبات مقسوماً على عدد الزيارات'
        UNION ALL SELECT 'sa', 'orders_divided_by_visits', 'عدد الطلبات مقسوماً على عدد الزيارات'
        UNION ALL SELECT 'en', 'source', 'Source'
        UNION ALL SELECT 'ar', 'source', 'المصدر'
        UNION ALL SELECT 'sa', 'source', 'المصدر'
        UNION ALL SELECT 'en', 'share_of_visits', 'Share of visits'
        UNION ALL SELECT 'ar', 'share_of_visits', 'الحصة من الزيارات'
        UNION ALL SELECT 'sa', 'share_of_visits', 'الحصة من الزيارات'
        UNION ALL SELECT 'en', 'sources_performance', 'Sources Performance'
        UNION ALL SELECT 'ar', 'sources_performance', 'أداء المصادر'
        UNION ALL SELECT 'sa', 'sources_performance', 'أداء المصادر'
        UNION ALL SELECT 'en', 'top_referrers_and_campaigns', 'Top referrers and campaigns'
        UNION ALL SELECT 'ar', 'top_referrers_and_campaigns', 'أهم المواقع المُحيلة والحملات'
        UNION ALL SELECT 'sa', 'top_referrers_and_campaigns', 'أهم المواقع المُحيلة والحملات'
        UNION ALL SELECT 'en', 'referrer', 'Referrer'
        UNION ALL SELECT 'ar', 'referrer', 'الموقع المُحيل'
        UNION ALL SELECT 'sa', 'referrer', 'الموقع المُحيل'
        UNION ALL SELECT 'en', 'campaign', 'Campaign'
        UNION ALL SELECT 'ar', 'campaign', 'الحملة'
        UNION ALL SELECT 'sa', 'campaign', 'الحملة'
        UNION ALL SELECT 'en', 'no_visits_recorded_in_this_period', 'No visits recorded in this period'
        UNION ALL SELECT 'ar', 'no_visits_recorded_in_this_period', 'لا توجد زيارات مسجّلة في هذه الفترة'
        UNION ALL SELECT 'sa', 'no_visits_recorded_in_this_period', 'لا توجد زيارات مسجّلة في هذه الفترة'
        UNION ALL SELECT 'en', 'tracking_starts_from_the_day_this_report_was_installed_earlier_visits_and_orders_are_not_included', 'Tracking starts from the day this report was installed; earlier visits and orders are not included'
        UNION ALL SELECT 'ar', 'tracking_starts_from_the_day_this_report_was_installed_earlier_visits_and_orders_are_not_included', 'التتبّع يبدأ من يوم تفعيل هذا التقرير؛ الزيارات والطلبات الأقدم غير مشمولة'
        UNION ALL SELECT 'sa', 'tracking_starts_from_the_day_this_report_was_installed_earlier_visits_and_orders_are_not_included', 'التتبّع يبدأ من يوم تفعيل هذا التقرير؛ الزيارات والطلبات الأقدم غير مشمولة'
        UNION ALL SELECT 'en', 'the_tracking_tables_are_not_installed_yet', 'The tracking tables are not installed yet'
        UNION ALL SELECT 'ar', 'the_tracking_tables_are_not_installed_yet', 'جداول التتبّع غير مثبّتة بعد'
        UNION ALL SELECT 'sa', 'the_tracking_tables_are_not_installed_yet', 'جداول التتبّع غير مثبّتة بعد'
) s
WHERE NOT EXISTS (
    SELECT 1 FROM `translations` t WHERE t.lang = s.lang AND t.lang_key = s.lang_key
);
