<?php

namespace Modules\ClinicCore\Database\Seeders;

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

class SoapOptionsSeeder extends Seeder
{
    public function run()
    {
        $rows = [];
        $now  = now();

        // ── S — Chief Complaints (species-specific) ──────────────────────────
        $complaints = [
            'all'    => ['Vomiting','Diarrhea','Not eating','Lethargy','Weight loss','Limping','Itching','Coughing','Sneezing','Eye discharge','Nasal discharge','Breathing difficulty','Seizure','Bloody stool','Excessive thirst'],
            'dog'    => ['Excessive barking','Pawing at ears','Dragging hindquarters','Licking paws','Collar rubbing','Anal scooting'],
            'cat'    => ['Hiding','Over-grooming','Straining to urinate','Hairballs','Chin acne','Drooling'],
            'rabbit' => ['GI stasis','Head tilt','Dental issues','Hair loss','Myiasis','Anorexia'],
            'bird'   => ['Feather plucking','Fluffed feathers','Regurgitation','Tail bobbing','Voice change','Beak malformation'],
        ];
        $i = 0;
        foreach ($complaints as $species => $items) {
            foreach ($items as $label) {
                $rows[] = ['section' => 'complaint', 'species' => $species, 'label' => $label, 'sort_order' => $i++, 'status' => 'active', 'business_id' => null, 'created_at' => $now, 'updated_at' => $now];
            }
        }

        // ── I — Lab Tests (species-specific) ─────────────────────────────────
        $labs = [
            'all'    => ['CBC (Complete Blood Count)','Blood Glucose','Urinalysis','Liver Panel (ALT/AST/ALP)','Kidney Panel (BUN/Creatinine)','Thyroid (T4)','Culture & Sensitivity','Fecal Float','Direct Smear','Stool Exam','Electrolytes','Serum Protein','Coagulation Panel','Blood Pressure'],
            'dog'    => ['Heartworm Antigen Test','Brucellosis Test','Parvovirus PCR','Distemper Titer','Lyme SNAP Test','Ehrlichia/Anaplasma Panel'],
            'cat'    => ['FeLV/FIV Combo Test','Feline Panleukopenia PCR','Corona Virus Titer','Bile Acids','Toxoplasmosis Titer'],
            'rabbit' => ['E. cuniculi Titer','Rabbit Calicivirus Titer','Myxomatosis Titer'],
            'bird'   => ['Chlamydophila PCR','Gram Stain','Newcastle Disease Titer','Polyomavirus PCR','PBFD PCR'],
        ];
        $i = 0;
        foreach ($labs as $species => $items) {
            foreach ($items as $label) {
                $rows[] = ['section' => 'lab_test', 'species' => $species, 'label' => $label, 'sort_order' => $i++, 'status' => 'active', 'business_id' => null, 'created_at' => $now, 'updated_at' => $now];
            }
        }

        // ── I — Physical Exams (species-specific) ────────────────────────────
        $exams = [
            'all'    => ['X-Ray (Radiograph)','Ultrasound','Otoscopy','Ophthalmoscopy','ECG','Biopsy','Fine Needle Aspirate','Blood Pressure Measurement'],
            'dog'    => ['Skin Scraping','Vaginal Cytology','Rectal Exam','Hip Evaluation (PennHIP)','Joint Fluid Analysis'],
            'cat'    => ['Tonometry (Eye Pressure)','Rectal Exam','Thoracocentesis'],
            'rabbit' => ['Dental X-Ray','Skull X-Ray','Slit Lamp Exam'],
            'bird'   => ['Endoscopy','Laparoscopy','Crop Flush'],
        ];
        $i = 0;
        foreach ($exams as $species => $items) {
            foreach ($items as $label) {
                $rows[] = ['section' => 'exam_test', 'species' => $species, 'label' => $label, 'sort_order' => $i++, 'status' => 'active', 'business_id' => null, 'created_at' => $now, 'updated_at' => $now];
            }
        }

        // ── P — Frequency Options ─────────────────────────────────────────────
        $frequencies = [
            'Once daily','Twice daily (BID)','3× daily (TID)','4× daily (QID)',
            'Every 8 hours','Every 12 hours','Every 48 hours',
            'As needed (PRN)','Single dose','Weekly','Bi-weekly','Monthly',
            'With food','Without food','Before meals','After meals',
        ];
        foreach ($frequencies as $j => $label) {
            $rows[] = ['section' => 'frequency', 'species' => 'all', 'label' => $label, 'sort_order' => $j, 'status' => 'active', 'business_id' => null, 'created_at' => $now, 'updated_at' => $now];
        }

        // ── O — Body Systems (ordered within groups) ─────────────────────────
        $systems = [
            ['label' => 'Cardiovascular',   'sort_order' => 0,  'group_name' => 'Cardio-Respiratory'],
            ['label' => 'Respiratory',      'sort_order' => 1,  'group_name' => 'Cardio-Respiratory'],
            ['label' => 'Gastrointestinal', 'sort_order' => 2,  'group_name' => 'Digestive & Metabolic'],
            ['label' => 'Endocrine',        'sort_order' => 3,  'group_name' => 'Digestive & Metabolic'],
            ['label' => 'Dental / Oral',    'sort_order' => 4,  'group_name' => 'Digestive & Metabolic'],
            ['label' => 'Musculoskeletal',  'sort_order' => 5,  'group_name' => 'Musculo-Neurological'],
            ['label' => 'Neurological',     'sort_order' => 6,  'group_name' => 'Musculo-Neurological'],
            ['label' => 'Clinictological',   'sort_order' => 7,  'group_name' => 'Integumentary & Sensory'],
            ['label' => 'Ophthalmological', 'sort_order' => 8,  'group_name' => 'Integumentary & Sensory'],
            ['label' => 'Ears / ENT',       'sort_order' => 9,  'group_name' => 'Integumentary & Sensory'],
            ['label' => 'Urogenital',       'sort_order' => 10, 'group_name' => 'Urogenital & Lymphatic'],
            ['label' => 'Lymphatic',        'sort_order' => 11, 'group_name' => 'Urogenital & Lymphatic'],
        ];
        foreach ($systems as $sys) {
            $rows[] = array_merge($sys, ['section' => 'body_system', 'species' => 'all', 'status' => 'active', 'business_id' => null, 'created_at' => $now, 'updated_at' => $now]);
        }

        // Insert only rows that don't already exist (idempotent)
        foreach ($rows as $row) {
            $exists = DB::table('clc_soap_options')
                ->whereNull('business_id')
                ->where('section', $row['section'])
                ->where('species', $row['species'])
                ->where('label', $row['label'])
                ->exists();
            if (!$exists) {
                DB::table('clc_soap_options')->insert($row);
            }
        }

        // Backfill group_name for existing body_system rows that have no group assigned
        foreach ($systems as $sys) {
            DB::table('clc_soap_options')
                ->whereNull('business_id')
                ->where('section', 'body_system')
                ->where('label', $sys['label'])
                ->whereNull('group_name')
                ->update(['group_name' => $sys['group_name'], 'sort_order' => $sys['sort_order']]);
        }
    }
}
