<?php

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

/**
 * Seed additional default complaint chips so common clinical signs the
 * AI matcher needs to land have a home. Original seeder only had ~15
 * complaints; this adds another 18 covering vitals abnormalities,
 * urinary signs, GI emergencies, dehydration, collapse, and more.
 *
 * Idempotent: skips any label that already exists for species='all'
 * at the system level (business_id NULL).
 */
class SeedAdditionalComplaintChips extends Migration
{
    public function up()
    {
        if (!\Schema::hasTable('vc_soap_options')) return;

        $now = now();
        // Get the current max sort_order for system-default complaints
        // so new rows append at the end.
        $start = (int) DB::table('vc_soap_options')
            ->where('section', 'complaint')
            ->whereNull('business_id')
            ->max('sort_order');

        $newLabels = [
            // Vital-sign red flags
            'Hypothermia (low temp)',
            'Fever / hyperthermia',
            // Cardiovascular / shock
            'Collapse / syncope',
            'Pale gums',
            'Cyanosis (blue gums)',
            'Icterus (jaundice)',
            // Urinary
            'Anuria / not urinating',
            'Polyuria (increased urine)',
            'Polydipsia (increased thirst)',
            'Haematuria (blood in urine)',
            'Urinary incontinence',
            // GI / abdominal
            'Anorexia (won\'t eat)',
            'Hyporexia (eating less)',
            'Regurgitation',
            'Constipation',
            'Tenesmus (straining to defecate)',
            'Hypersalivation / drooling',
            // Hydration / weight
            'Dehydration',
            // Behavioural
            'Hiding / withdrawn',
            'Vocalising in pain',
            // Neurological extras
            'Disorientation / confusion',
            'Head pressing',
            // Respiratory extras
            'Open-mouth breathing',
            // Skin extras
            'Skin mass / lump',
        ];

        $rows = [];
        foreach ($newLabels as $label) {
            $exists = DB::table('vc_soap_options')
                ->where('section', 'complaint')
                ->whereNull('business_id')
                ->where('species', 'all')
                ->where('label', $label)
                ->exists();
            if ($exists) continue;
            $rows[] = [
                'business_id' => null,
                'section'     => 'complaint',
                'species'     => 'all',
                'label'       => $label,
                'sort_order'  => ++$start,
                'status'      => 'active',
                'created_at'  => $now,
                'updated_at'  => $now,
            ];
        }
        if ($rows) {
            DB::table('vc_soap_options')->insert($rows);
        }
    }

    public function down()
    {
        // Don't auto-remove — clinics may have customised them.
    }
}
