<?php

namespace Modules\MedNotify\Database\Seeders;

use Illuminate\Database\Seeder;
use Modules\MedNotify\Entities\MednotifyEvent;

/**
 * Seeds the 5 SYSTEM events from the spec (§7). business_id = NULL so they are
 * available to every tenant; a tenant may add per-business overrides later.
 * Idempotent via updateOrCreate on (business_id NULL, event_key).
 *
 * Other modules register their own events the same way (see README). These five
 * are the canonical examples the notifications spec calls out.
 */
class MednotifyEventsSeeder extends Seeder
{
    public function run(): void
    {
        foreach ($this->events() as $event) {
            MednotifyEvent::updateOrCreate(
                ['business_id' => null, 'event_key' => $event['event_key']],
                [
                    'module_key'          => $event['module_key'],
                    'label'               => $event['label'],
                    'description'         => $event['description'],
                    'available_variables' => $event['available_variables'],
                    'default_channels'    => $event['default_channels'],
                    'is_active'           => true,
                ]
            );
        }
    }

    /** @return array<int, array<string, mixed>> */
    private function events(): array
    {
        return [
            [
                'event_key'   => 'appointment.created',
                'module_key'  => 'MedNotify',
                'label'       => 'Appointment created',
                'description' => 'A new appointment was scheduled for the patient.',
                'default_channels' => ['in_app', 'sms'],
                'available_variables' => [
                    ['name' => 'patient_name',     'label' => 'Patient name'],
                    ['name' => 'appointment_date', 'label' => 'Appointment date/time'],
                    ['name' => 'doctor_name',      'label' => 'Provider name'],
                    ['name' => 'facility_name',    'label' => 'Facility name'],
                    ['name' => 'link',             'label' => 'Deep link'],
                ],
            ],
            [
                'event_key'   => 'lab.result.ready',
                'module_key'  => 'MedNotify',
                'label'       => 'Lab result ready',
                'description' => 'A lab result is available for the ordering doctor and the patient.',
                'default_channels' => ['in_app', 'email'],
                'available_variables' => [
                    ['name' => 'patient_name', 'label' => 'Patient name'],
                    ['name' => 'test_name',    'label' => 'Test / panel name'],
                    ['name' => 'order_no',     'label' => 'Order number'],
                    ['name' => 'link',         'label' => 'Deep link'],
                ],
            ],
            [
                'event_key'   => 'prescription.ready',
                'module_key'  => 'MedNotify',
                'label'       => 'Prescription ready',
                'description' => 'A prescription is ready for pickup / dispensing.',
                'default_channels' => ['in_app', 'sms'],
                'available_variables' => [
                    ['name' => 'patient_name',  'label' => 'Patient name'],
                    ['name' => 'pharmacy_name', 'label' => 'Pharmacy name'],
                    ['name' => 'rx_no',         'label' => 'Prescription number'],
                    ['name' => 'link',          'label' => 'Deep link'],
                ],
            ],
            [
                'event_key'   => 'invoice.overdue',
                'module_key'  => 'MedNotify',
                'label'       => 'Invoice overdue',
                'description' => 'A patient invoice has passed its due date.',
                'default_channels' => ['in_app', 'email', 'sms'],
                'available_variables' => [
                    ['name' => 'patient_name', 'label' => 'Patient name'],
                    ['name' => 'invoice_no',   'label' => 'Invoice number'],
                    ['name' => 'amount_due',   'label' => 'Amount due'],
                    ['name' => 'due_date',     'label' => 'Due date'],
                    ['name' => 'link',         'label' => 'Deep link'],
                ],
            ],
            [
                'event_key'   => 'bed.available',
                'module_key'  => 'MedNotify',
                'label'       => 'Bed available',
                'description' => 'A bed matching a pending admission / reservation is now available.',
                'default_channels' => ['in_app'],
                'available_variables' => [
                    ['name' => 'ward_name',   'label' => 'Ward name'],
                    ['name' => 'bed_no',      'label' => 'Bed number'],
                    ['name' => 'patient_name', 'label' => 'Patient name'],
                    ['name' => 'link',        'label' => 'Deep link'],
                ],
            ],
        ];
    }
}
