<?php
declare(strict_types=1);

use App\Core\Database;

return function (Database $db): void {
    $triggers = [
        // ─── Event-driven triggers ──────────────────────────────────────────────────
        [
            'trigger_code'    => 'session_cancelled',
            'trigger_name_ar' => 'إخطار إلغاء حصة تدريبية',
            'event_name'      => 'session.cancelled',
            'channel'         => 'sms',
            'recipient_type'  => 'session_players',
            'priority'        => 10,
            'config_json'     => json_encode(['delay_minutes' => 0, 'max_attempts' => 3]),
        ],
        [
            'trigger_code'    => 'session_rescheduled',
            'trigger_name_ar' => 'إخطار إعادة جدولة حصة',
            'event_name'      => 'session.rescheduled',
            'channel'         => 'sms',
            'recipient_type'  => 'session_players',
            'priority'        => 10,
            'config_json'     => json_encode(['delay_minutes' => 0, 'max_attempts' => 3]),
        ],
        [
            'trigger_code'    => 'player_transferred',
            'trigger_name_ar' => 'إخطار نقل لاعب لمجموعة أخرى',
            'event_name'      => 'group.player_transferred',
            'channel'         => 'sms',
            'recipient_type'  => 'player',
            'priority'        => 5,
            'config_json'     => null,
        ],
        [
            'trigger_code'    => 'reservation_confirmed',
            'trigger_name_ar' => 'تأكيد حجز ملعب',
            'event_name'      => 'reservation.confirmed',
            'channel'         => 'sms',
            'recipient_type'  => 'booker',
            'priority'        => 5,
            'config_json'     => null,
        ],
        [
            'trigger_code'    => 'reservation_reminder',
            'trigger_name_ar' => 'تذكير بحجز غداً',
            'event_name'      => 'reservation.reminder',
            'channel'         => 'sms',
            'recipient_type'  => 'direct',
            'priority'        => 5,
            'config_json'     => null,
        ],
        [
            'trigger_code'    => 'carnet_low_balance',
            'trigger_name_ar' => 'تنبيه رصيد كارنيه منخفض',
            'event_name'      => 'carnet.low_balance',
            'channel'         => 'sms',
            'recipient_type'  => 'member',
            'priority'        => 5,
            'config_json'     => null,
        ],
        [
            'trigger_code'    => 'carnet_guest_entry',
            'trigger_name_ar' => 'إخطار دخول ضيف كارنيه',
            'event_name'      => 'carnet_guest.entry_recorded',
            'channel'         => 'sms',
            'recipient_type'  => 'member',
            'priority'        => 0,
            'is_active'       => 0, // Optional: disabled by default
            'config_json'     => null,
        ],
        [
            'trigger_code'    => 'coach_payment',
            'trigger_name_ar' => 'إخطار صرف مستحقات مدرب',
            'event_name'      => 'coach.payment_processed',
            'channel'         => 'sms',
            'recipient_type'  => 'coach',
            'priority'        => 5,
            'config_json'     => null,
        ],
        [
            'trigger_code'    => 'player_evaluation',
            'trigger_name_ar' => 'إخطار تقييم لاعب',
            'event_name'      => 'player.evaluation_submitted',
            'channel'         => 'sms',
            'recipient_type'  => 'player_and_parent',
            'priority'        => 3,
            'config_json'     => null,
        ],
        [
            'trigger_code'    => 'tournament_registration',
            'trigger_name_ar' => 'إخطار فتح تسجيل بطولة',
            'event_name'      => 'tournament.registration_open',
            'channel'         => 'sms',
            'recipient_type'  => 'group_players',
            'priority'        => 5,
            'config_json'     => null,
        ],
        [
            'trigger_code'    => 'player_injury',
            'trigger_name_ar' => 'إخطار إصابة لاعب لولي الأمر',
            'event_name'      => 'player.injury_reported',
            'channel'         => 'sms',
            'recipient_type'  => 'parent',
            'priority'        => 100, // Highest priority
            'config_json'     => json_encode(['delay_minutes' => 0, 'max_attempts' => 5]),
        ],
        [
            'trigger_code'    => 'player_achievement',
            'trigger_name_ar' => 'إخطار إنجاز لاعب',
            'event_name'      => 'player.achievement_earned',
            'channel'         => 'sms',
            'recipient_type'  => 'player_and_parent',
            'priority'        => 3,
            'config_json'     => null,
        ],

        // ─── Cron-driven triggers (checked by CronNotificationService) ──────────────
        [
            'trigger_code'    => 'medical_expiry_7d',
            'trigger_name_ar' => 'تذكير انتهاء شهادة طبية (7 أيام)',
            'event_name'      => 'cron.medical_expiry',
            'channel'         => 'sms',
            'recipient_type'  => 'player',
            'priority'        => 5,
            'config_json'     => json_encode(['days_before' => 7]),
        ],
        [
            'trigger_code'    => 'medical_expiry_3d',
            'trigger_name_ar' => 'تذكير انتهاء شهادة طبية (3 أيام)',
            'event_name'      => 'cron.medical_expiry',
            'channel'         => 'sms',
            'recipient_type'  => 'player',
            'priority'        => 8,
            'config_json'     => json_encode(['days_before' => 3]),
        ],
        [
            'trigger_code'    => 'medical_expiry_today',
            'trigger_name_ar' => 'تنبيه انتهاء شهادة طبية اليوم',
            'event_name'      => 'cron.medical_expiry',
            'channel'         => 'sms',
            'recipient_type'  => 'player',
            'priority'        => 10,
            'config_json'     => json_encode(['days_before' => 0]),
        ],
        [
            'trigger_code'    => 'subscription_payment_5d',
            'trigger_name_ar' => 'تذكير سداد اشتراك (5 أيام)',
            'event_name'      => 'cron.subscription_payment',
            'channel'         => 'sms',
            'recipient_type'  => 'player',
            'priority'        => 5,
            'config_json'     => json_encode(['days_before' => 5]),
        ],
        [
            'trigger_code'    => 'subscription_payment_today',
            'trigger_name_ar' => 'تذكير سداد اشتراك اليوم',
            'event_name'      => 'cron.subscription_payment',
            'channel'         => 'sms',
            'recipient_type'  => 'player',
            'priority'        => 8,
            'config_json'     => json_encode(['days_before' => 0]),
        ],
        [
            'trigger_code'    => 'subscription_payment_overdue',
            'trigger_name_ar' => 'تنبيه تأخر سداد اشتراك',
            'event_name'      => 'cron.subscription_payment',
            'channel'         => 'sms',
            'recipient_type'  => 'player',
            'priority'        => 10,
            'config_json'     => json_encode(['days_overdue' => 1]),
        ],
        [
            'trigger_code'    => 'membership_renewal_30d',
            'trigger_name_ar' => 'تذكير تجديد عضوية (30 يوم)',
            'event_name'      => 'cron.membership_renewal',
            'channel'         => 'sms',
            'recipient_type'  => 'member',
            'priority'        => 3,
            'config_json'     => json_encode(['days_before' => 30]),
        ],
        [
            'trigger_code'    => 'membership_renewal_7d',
            'trigger_name_ar' => 'تذكير تجديد عضوية (7 أيام)',
            'event_name'      => 'cron.membership_renewal',
            'channel'         => 'sms',
            'recipient_type'  => 'member',
            'priority'        => 8,
            'config_json'     => json_encode(['days_before' => 7]),
        ],
        [
            'trigger_code'    => 'coach_monthly_metrics',
            'trigger_name_ar' => 'ملخص أداء المدرب الشهري',
            'event_name'      => 'cron.coach_metrics',
            'channel'         => 'sms',
            'recipient_type'  => 'coach',
            'priority'        => 0,
            'config_json'     => json_encode(['frequency' => 'monthly']),
        ],
    ];

    $now = date('Y-m-d H:i:s');

    foreach ($triggers as $trigger) {
        // Check if already exists
        $existing = $db->selectOne(
            "SELECT id FROM notification_triggers WHERE trigger_code = ?",
            [$trigger['trigger_code']]
        );
        if ($existing) {
            continue;
        }

        $db->insert('notification_triggers', array_merge($trigger, [
            'template_id' => null, // Templates must be configured by admin
            'is_active'   => $trigger['is_active'] ?? 1,
            'created_at'  => $now,
            'updated_at'  => $now,
        ]));
    }
};
