<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class PaymentNotificationTemplateSeeder extends Seeder
{
    public function run(): void
    {
        $now = now();

        $templates = [
            // Email template
            [
                'uuid' => Str::uuid()->toString(),
                'academy_id' => null,
                'event_type' => 'payment.received',
                'channel' => 'email',
                'subject' => 'تأكيد دفع — {{amount}} ج.م',
                'body' => "مرحباً {{participant_name}},\n\nتم تسجيل دفعة بقيمة {{amount}} ج.م بتاريخ {{date}}.\nطريقة الدفع: {{method}}\nرقم الإيصال: {{receipt_number}}\n\nشكراً لك،\nإدارة الأكاديمية",
                'variables' => json_encode(['participant_name', 'amount', 'method', 'date', 'receipt_number', 'invoice_number']),
                'is_active' => true,
                'locale' => 'ar',
                'metadata' => '{}',
                'created_at' => $now,
                'updated_at' => $now,
            ],
            // SMS template
            [
                'uuid' => Str::uuid()->toString(),
                'academy_id' => null,
                'event_type' => 'payment.received',
                'channel' => 'sms',
                'subject' => null,
                'body' => 'تم استلام {{amount}} ج.م - إيصال: {{receipt_number}} - {{date}}',
                'variables' => json_encode(['amount', 'receipt_number', 'date']),
                'is_active' => true,
                'locale' => 'ar',
                'metadata' => '{}',
                'created_at' => $now,
                'updated_at' => $now,
            ],
            // In-App template
            [
                'uuid' => Str::uuid()->toString(),
                'academy_id' => null,
                'event_type' => 'payment.received',
                'channel' => 'in_app',
                'subject' => null,
                'body' => 'تم تسجيل دفعة {{amount}} ج.م من {{participant_name}}',
                'variables' => json_encode(['amount', 'participant_name']),
                'is_active' => true,
                'locale' => 'ar',
                'metadata' => '{}',
                'created_at' => $now,
                'updated_at' => $now,
            ],
        ];

        foreach ($templates as $template) {
            DB::table('notification_templates')->updateOrInsert(
                [
                    'academy_id' => $template['academy_id'],
                    'event_type' => $template['event_type'],
                    'channel' => $template['channel'],
                    'locale' => $template['locale'],
                ],
                $template,
            );
        }

        $this->command->info('Seeded 3 payment notification templates (email, sms, in_app).');
    }
}
