<?php

namespace Database\Seeders;

use App\Models\NotificationEvent;
use App\Models\NotificationTemplate;
use Illuminate\Database\Seeder;

class NotificationSeeder extends Seeder
{
    public function run(): void
    {
        $events = [
            'customer.registration' => ['Registrasi Customer', 'customer', 'Halo {{customer.name}}, registrasi Anda di {{tenant.name}} berhasil diterima.'],
            'customer.activation' => ['Aktivasi Customer', 'customer', 'Halo {{customer.name}}, layanan Anda telah aktif. ID pelanggan: {{customer.customer_code}}.'],
            'billing.invoice_sent' => ['Kirim Tagihan', 'billing', 'Tagihan {{invoice.invoice_number}} sebesar {{invoice.total}} telah diterbitkan. Batas isolir: {{invoice.isolation_date}}.'],
            'billing.warning' => ['Peringatan Tagihan', 'billing', 'Pengingat: tagihan {{invoice.invoice_number}} sebesar {{invoice.balance}} belum dibayar.'],
            'billing.isolated' => ['Isolir Pelanggan', 'billing', 'Layanan {{customer.customer_code}} memasuki masa isolir karena tagihan belum dibayar.'],
            'voucher_agent.registration' => ['Registrasi Agen Voucher', 'voucher', 'Registrasi agen voucher {{agent.name}} berhasil.'],
            'voucher_agent.balance_added' => ['Tambah Saldo Agen Voucher', 'voucher', 'Saldo agen {{agent.name}} bertambah {{balance.amount}}. Saldo saat ini {{balance.current}}.'],
        ];
        $channels = ['system', 'whatsapp_official', 'whatsapp_unofficial', 'email', 'telegram'];

        foreach ($events as $code => [$name, $category, $body]) {
            $event = NotificationEvent::updateOrCreate(
                ['code' => $code],
                [
                    'name' => $name,
                    'category' => $category,
                    'description' => "Pesan otomatis untuk {$name}",
                    'available_channels' => $channels,
                    'variables' => $this->variables($body),
                    'is_system' => true,
                    'enabled' => true,
                ],
            );
            foreach ($channels as $channel) {
                NotificationTemplate::updateOrCreate(
                    [
                        'tenant_id' => null,
                        'notification_event_id' => $event->id,
                        'channel' => $channel,
                        'language' => 'id',
                        'version' => 1,
                    ],
                    [
                        'name' => "{$name} - {$this->channelName($channel)}",
                        'subject' => $channel === 'email' || $channel === 'system' ? $name : null,
                        'body' => $body,
                        'approval_status' => $channel === 'whatsapp_official' ? 'pending' : 'not_required',
                        'enabled' => true,
                        'variables' => $this->variables($body),
                    ],
                );
            }
        }
    }

    private function variables(string $body): array
    {
        preg_match_all('/{{\s*([a-zA-Z0-9_.]+)\s*}}/', $body, $matches);

        return array_values(array_unique($matches[1]));
    }

    private function channelName(string $channel): string
    {
        return match ($channel) {
            'system' => 'Aplikasi',
            'whatsapp_official' => 'WhatsApp Official',
            'whatsapp_unofficial' => 'WhatsApp Unofficial',
            'email' => 'Email',
            'telegram' => 'Telegram',
        };
    }
}
