<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;

class AddWhatsappMarketingQueueSettingsKeys extends Migration
{
    public function up()
    {
        $rows = [
            // التوقيت/الدفعات
            ['key' => 'whatsapp_marketing_delay', 'name' => 'whatsapp_marketing_delay', 'value' => '1'],          // ثواني بين كل رسالة
            ['key' => 'whatsapp_marketing_delay_jitter', 'name' => 'whatsapp_marketing_delay_jitter', 'value' => '1'], // عشوائية بسيطة (ثواني)
            ['key' => 'whatsapp_marketing_batch', 'name' => 'whatsapp_marketing_batch', 'value' => '10'],         // عدد الرسائل في الدفعة
            ['key' => 'whatsapp_marketing_pause', 'name' => 'whatsapp_marketing_pause', 'value' => '10'],         // ثواني توقف بعد كل دفعة

            // المحاولات والمهلة
            ['key' => 'whatsapp_marketing_max_retries', 'name' => 'whatsapp_marketing_max_retries', 'value' => '2'],
            ['key' => 'whatsapp_marketing_retry_delay', 'name' => 'whatsapp_marketing_retry_delay', 'value' => '10'],
            ['key' => 'whatsapp_marketing_timeout', 'name' => 'whatsapp_marketing_timeout', 'value' => '30'],

            // الجلسة
            ['key' => 'whatsapp_marketing_max_per_run', 'name' => 'whatsapp_marketing_max_per_run', 'value' => '100'],
            ['key' => 'whatsapp_marketing_session_pause', 'name' => 'whatsapp_marketing_session_pause', 'value' => '900'], // 15 دقيقة

            // تقليل البلوك: منع التكرار لنفس العميل
            ['key' => 'whatsapp_marketing_min_gap_minutes', 'name' => 'whatsapp_marketing_min_gap_minutes', 'value' => '1440'], // 24 ساعة
        ];

        foreach ($rows as $row) {
            DB::table('settings')->updateOrInsert(['key' => $row['key']], $row);
        }
    }

    public function down()
    {
        DB::table('settings')->whereIn('key', [
            'whatsapp_marketing_delay',
            'whatsapp_marketing_delay_jitter',
            'whatsapp_marketing_batch',
            'whatsapp_marketing_pause',
            'whatsapp_marketing_max_retries',
            'whatsapp_marketing_retry_delay',
            'whatsapp_marketing_timeout',
            'whatsapp_marketing_max_per_run',
            'whatsapp_marketing_session_pause',
            'whatsapp_marketing_min_gap_minutes',
        ])->delete();
    }
}