<?php
declare(strict_types=1);

use App\Core\Database;

return function (Database $db): void {
    $rules = [
        [
            'code'                   => 'MEDICAL_CERT_EXPIRY',
            'name_ar'                => 'انتهاء الشهادة الطبية',
            'category'               => 'medical',
            'trigger_type'           => 'expiry',
            'trigger_config_json'    => json_encode(['days_before' => 30]),
            'notification_channels'  => 'system,sms',
            'recipient_roles_json'   => json_encode(['sports_admin']),
            'is_active'              => 1,
        ],
        [
            'code'                   => 'CONTRACT_EXPIRY',
            'name_ar'                => 'انتهاء عقد أكاديمية',
            'category'               => 'contracts',
            'trigger_type'           => 'expiry',
            'trigger_config_json'    => json_encode(['days_before' => 30]),
            'notification_channels'  => 'system',
            'recipient_roles_json'   => json_encode(['finance', 'academy_manager']),
            'is_active'              => 1,
        ],
        [
            'code'                   => 'REVENUE_BELOW_MINIMUM',
            'name_ar'                => 'إيرادات أقل من الحد الأدنى',
            'category'               => 'financial',
            'trigger_type'           => 'threshold',
            'trigger_config_json'    => json_encode(['threshold_pct' => 80]),
            'notification_channels'  => 'system',
            'recipient_roles_json'   => json_encode(['finance', 'academy_manager']),
            'is_active'              => 1,
        ],
        [
            'code'                   => 'ATTENDANCE_DROPPING',
            'name_ar'                => 'تراجع حضور اللاعب',
            'category'               => 'attendance',
            'trigger_type'           => 'threshold',
            'trigger_config_json'    => json_encode(['consecutive_absences' => 3]),
            'notification_channels'  => 'system,sms',
            'recipient_roles_json'   => json_encode(['coach', 'guardian']),
            'is_active'              => 1,
        ],
        [
            'code'                   => 'GROUP_NEAR_CAPACITY',
            'name_ar'                => 'مجموعة قاربت على الامتلاء',
            'category'               => 'capacity',
            'trigger_type'           => 'threshold',
            'trigger_config_json'    => json_encode(['capacity_pct' => 90]),
            'notification_channels'  => 'system',
            'recipient_roles_json'   => json_encode(['sports_admin']),
            'is_active'              => 1,
        ],
        [
            'code'                   => 'SUBSCRIPTION_OVERDUE',
            'name_ar'                => 'اشتراك متأخر السداد',
            'category'               => 'subscriptions',
            'trigger_type'           => 'threshold',
            'trigger_config_json'    => json_encode(['days_overdue' => 7]),
            'notification_channels'  => 'system,sms',
            'recipient_roles_json'   => json_encode(['finance']),
            'is_active'              => 1,
        ],
        [
            'code'                   => 'POOL_SAFETY_THRESHOLD',
            'name_ar'                => 'حد أمان حمام السباحة',
            'category'               => 'safety',
            'trigger_type'           => 'threshold',
            'trigger_config_json'    => json_encode(['max_occupancy_pct' => 100]),
            'notification_channels'  => 'system',
            'recipient_roles_json'   => json_encode(['pool_admin']),
            'is_active'              => 1,
        ],
        [
            'code'                   => 'SETTLEMENT_DUE',
            'name_ar'                => 'موعد التسوية المالية',
            'category'               => 'financial',
            'trigger_type'           => 'schedule',
            'trigger_config_json'    => json_encode(['on_settlement_day' => true]),
            'notification_channels'  => 'system',
            'recipient_roles_json'   => json_encode(['finance']),
            'is_active'              => 1,
        ],
        [
            'code'                   => 'WAITING_LIST_SPOT',
            'name_ar'                => 'إتاحة مكان من قائمة الانتظار',
            'category'               => 'capacity',
            'trigger_type'           => 'threshold',
            'trigger_config_json'    => json_encode(['notify_on_spot_open' => true]),
            'notification_channels'  => 'system,sms',
            'recipient_roles_json'   => json_encode(['player']),
            'is_active'              => 1,
        ],
    ];

    foreach ($rules as $rule) {
        $exists = $db->selectOne(
            "SELECT id FROM alert_rules WHERE code = ?",
            [$rule['code']]
        );
        if (!$exists) {
            $db->insert('alert_rules', $rule);
        }
    }
};
