<?php

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

/**
 * Item #1 - comprehensive system-default vital ranges across the species
 * a small-animal clinic actually sees. The original seed only covered
 * canine + feline (all-ages + pediatric); this migration expands to
 * every life-stage for dog & cat, then adds rabbit, ferret, bird,
 * guinea pig, rat/mouse, reptile and small/large dog variants so the
 * inline range hints + critical-check report are useful from day one.
 *
 * Values reflect commonly-cited clinical references (Merck Vet Manual,
 * Plumb's, BSAVA). Clinics override per-business via the admin UI.
 *
 * Idempotent: every row check uses an exists() guard so re-runs after
 * the original seeder don't duplicate.
 */
class ClcSeedComprehensiveVitalRanges extends Migration
{
    public function up()
    {
        $now = now();

        // species, life_stage, temp_low, temp_high, hr_low, hr_high, rr_low, rr_high, wt_low_kg, wt_high_kg
        $rows = [
            // ── CANINE - by life stage ─────────────────────────────────
            ['canine', 'pediatric', 38.5, 39.5,  80, 220, 15, 35,  null, null], // <1 yr puppy
            ['canine', 'adult',     38.3, 39.2,  60, 140, 10, 30,  null, null], // 1-7 yr
            ['canine', 'senior',    37.8, 39.0,  50, 130,  8, 28,  null, null], // 8+ yr
            ['canine', 'all',       38.3, 39.2,  60, 140, 10, 30,  null, null],

            // ── CANINE SIZE VARIANTS (custom species keys; doctors who
            //     want size-specific bounds can pick the matching species
            //     name on the pet record). Toy/small dogs have higher HR. ──
            ['canine_toy',   'adult', 38.3, 39.2,  90, 160, 15, 35,  0.5,  5.0],
            ['canine_small', 'adult', 38.3, 39.2,  80, 140, 15, 35,  5.0, 10.0],
            ['canine_med',   'adult', 38.3, 39.2,  70, 120, 10, 30, 10.0, 25.0],
            ['canine_large', 'adult', 38.3, 39.2,  60, 100, 10, 30, 25.0, 45.0],
            ['canine_giant', 'adult', 38.3, 39.2,  50,  90, 10, 30, 45.0, 90.0],

            // ── FELINE - by life stage ─────────────────────────────────
            ['feline', 'pediatric', 38.0, 39.5, 200, 260, 20, 45,  null, null], // <1 yr kitten
            ['feline', 'adult',     38.0, 39.2, 140, 220, 16, 40,  2.5,  6.5],
            ['feline', 'senior',    37.5, 39.0, 130, 220, 16, 40,  2.5,  6.5],
            ['feline', 'all',       38.0, 39.2, 140, 220, 16, 40,  2.5,  6.5],

            // ── RABBIT (lagomorph) - common exotic in small-animal clinics
            ['lagomorph', 'all',       38.5, 40.0, 180, 250, 30, 60,  1.0,  6.0],
            ['lagomorph', 'pediatric', 38.5, 40.5, 200, 280, 35, 65,  null, null],

            // ── FERRET (mustelid) - HR very high, low body weight
            ['mustelid', 'all', 37.8, 40.0, 200, 400, 33, 36,  0.6, 2.0],

            // ── BIRD (avian) - HR can be 250-600 depending on size,
            //     respiratory baseline highly species-specific. Use
            //     conservative pet-parrot / pigeon-sized defaults.
            ['avian', 'all', 39.5, 42.5, 150, 600, 25, 70, 0.020, 1.5],

            // ── RODENT (guinea pig / hamster / rat / mouse) -----------
            ['rodent', 'all', 37.5, 39.5, 230, 450, 60, 200, 0.030, 1.5],

            // ── REPTILE (lizard / snake / chelonian) — temp very
            //     species-and-environment dependent. Set null and rely
            //     on per-clinic overrides; HR/RR ranges generic.
            ['reptile', 'all', null, null,  30, 100, 10, 40, 0.050, 30.0],

            // ── EQUINE (horse) - large-animal; included for mixed clinics
            ['equine', 'adult', 37.5, 38.5, 28, 44, 8, 16, 300, 600],
            ['equine', 'pediatric', 37.5, 39.0, 60, 90, 14, 30, null, null],

            // ── BOVINE (cattle) - large-animal
            ['bovine', 'adult', 38.0, 39.3, 48, 84, 10, 30, 400, 800],

            // ── OVINE (sheep)
            ['ovine', 'adult', 38.5, 40.0, 60, 120, 12, 30, 30, 100],

            // ── CAPRINE (goat)
            ['caprine', 'adult', 38.5, 40.5, 60, 120, 15, 35, 25, 90],
        ];

        foreach ($rows as $r) {
            [$species, $stage, $tL, $tH, $hrL, $hrH, $rrL, $rrH, $wL, $wH] = $r;
            $exists = DB::table('clc_vital_ranges')
                ->whereNull('business_id')
                ->where('species', $species)->where('life_stage', $stage)
                ->exists();
            if ($exists) continue;
            DB::table('clc_vital_ranges')->insert([
                'business_id'           => null,
                'species'               => $species,
                'life_stage'            => $stage,
                'temperature_low'       => $tL,
                'temperature_high'      => $tH,
                'heart_rate_low'        => $hrL,
                'heart_rate_high'       => $hrH,
                'respiratory_rate_low'  => $rrL,
                'respiratory_rate_high' => $rrH,
                'weight_low_kg'         => $wL,
                'weight_high_kg'        => $wH,
                'created_at'            => $now,
                'updated_at'            => $now,
            ]);
        }
    }

    public function down()
    {
        // Don't auto-roll-back system defaults — clinics may have built
        // patient ages/critical reports against them. Manual cleanup
        // only if explicitly needed.
    }
}
