<?php

namespace Modules\MedBloodBank\Database\Seeders;

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

/**
 * Registers the `bloodbank.stock_low` event (+ system-default templates) with
 * MedNotify so blood-bank staff can be notified when a group drops below its
 * minimum.
 *
 * FAIL-SOFT: only writes into mednotify_events / mednotify_templates when those
 * tables exist (MedNotify installed). It NEVER writes to any core table and is a
 * clean no-op on hosts without MedNotify. Idempotent via updateOrInsert keyed on
 * (business_id NULL, event_key[, channel]). Mirrors MedbillNotifyEventSeeder.
 */
class MbbNotifyEventSeeder extends Seeder
{
    public function run(): void
    {
        if (! Schema::hasTable('mednotify_events')) {
            return; // MedNotify not installed — nothing to register.
        }

        try {
            DB::table('mednotify_events')->updateOrInsert(
                ['business_id' => null, 'event_key' => 'bloodbank.stock_low'],
                [
                    'module_key'          => 'MedBloodBank',
                    'label'               => 'Blood stock low',
                    'description'         => 'Available stock for a blood group dropped to or below its configured minimum.',
                    'available_variables' => json_encode([
                        ['name' => 'blood_group', 'label' => 'Blood group'],
                        ['name' => 'available',   'label' => 'Available bags'],
                        ['name' => 'min',         'label' => 'Minimum threshold'],
                        ['name' => 'link',        'label' => 'Deep link'],
                    ]),
                    'default_channels' => json_encode(['in_app', 'email']),
                    'is_active'        => true,
                    'updated_at'       => now(),
                    'created_at'       => now(),
                ]
            );

            if (Schema::hasTable('mednotify_templates')) {
                $templates = [
                    'in_app' => [
                        'subject' => 'Low blood stock: {blood_group}',
                        'body'    => 'Only {available} bag(s) of {blood_group} available (min {min}). Consider a donor recall.',
                    ],
                    'email' => [
                        'subject' => 'Low blood stock — {blood_group}',
                        'body'    => "Available stock of {blood_group} has dropped to {available} bag(s), at or below the minimum of {min}.\n{link}",
                    ],
                ];

                foreach ($templates as $channel => $tpl) {
                    DB::table('mednotify_templates')->updateOrInsert(
                        ['business_id' => null, 'event_key' => 'bloodbank.stock_low', 'channel' => $channel],
                        [
                            'subject'    => $tpl['subject'],
                            'body'       => $tpl['body'],
                            'enabled'    => true,
                            'updated_at' => now(),
                            'created_at' => now(),
                        ]
                    );
                }
            }
        } catch (\Throwable $e) {
            \Log::warning('MedBloodBank could not register bloodbank.stock_low with MedNotify: ' . $e->getMessage());
        }
    }
}
