<?php

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

/**
 * P5-T2 dental deep-dive — register the 'dental.recall_due' notification event
 * with MedNotify (system-scoped, business_id NULL) plus a default in-app
 * template so the channel actually sends. Mirrors ClinicCore's
 * seed_slot_available_mednotify_event migration. Entirely fail-soft: if
 * MedNotify is not installed the migration is a no-op, and raw DB inserts keep
 * this migration free of any hard dependency on MedNotify classes.
 */
class SeedDentalRecallMednotifyEvent extends Migration
{
    public function up()
    {
        if (! Schema::hasTable('mednotify_events') || ! Schema::hasTable('mednotify_templates')) {
            return; // MedNotify absent — nothing to register
        }

        try {
            $now = now();

            $eventExists = DB::table('mednotify_events')
                ->whereNull('business_id')->where('event_key', 'dental.recall_due')->exists();
            if (! $eventExists) {
                DB::table('mednotify_events')->insert([
                    'business_id'         => null,
                    'event_key'           => 'dental.recall_due',
                    'module_key'          => 'DentalClinic',
                    'label'               => 'Dental recall due',
                    'description'         => 'A dental recall (cleaning, periodontal maintenance, review…) has reached its due date.',
                    'default_channels'    => json_encode(['in_app']),
                    'available_variables' => json_encode([
                        ['name' => 'patient_name', 'label' => 'Patient name'],
                        ['name' => 'reason',       'label' => 'Recall reason'],
                        ['name' => 'due_date',     'label' => 'Due date'],
                        ['name' => 'link',         'label' => 'Link'],
                    ]),
                    'is_active'  => 1,
                    'created_at' => $now,
                    'updated_at' => $now,
                ]);
            }

            $tplExists = DB::table('mednotify_templates')
                ->whereNull('business_id')->where('event_key', 'dental.recall_due')
                ->where('channel', 'in_app')->exists();
            if (! $tplExists) {
                DB::table('mednotify_templates')->insert([
                    'business_id' => null,
                    'event_key'   => 'dental.recall_due',
                    'channel'     => 'in_app',
                    'subject'     => 'Dental recall due',
                    'body'        => 'Hi {patient_name}, your dental recall ({reason}) is due on {due_date}.',
                    'enabled'     => 1,
                    'created_at'  => $now,
                    'updated_at'  => $now,
                ]);
            }
        } catch (\Throwable $e) {
            Log::warning('DentalClinic: dental.recall_due MedNotify seed skipped: ' . $e->getMessage());
        }
    }

    public function down()
    {
        if (! Schema::hasTable('mednotify_events')) {
            return;
        }

        try {
            DB::table('mednotify_templates')->whereNull('business_id')
                ->where('event_key', 'dental.recall_due')->delete();
            DB::table('mednotify_events')->whereNull('business_id')
                ->where('event_key', 'dental.recall_due')->delete();
        } catch (\Throwable $e) {
            Log::warning('DentalClinic: dental.recall_due MedNotify unseed skipped: ' . $e->getMessage());
        }
    }
}
