<?php

namespace Modules\ClinicCore\Database\Seeders;

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

class DefaultMedicinesSeeder extends Seeder
{
    public function run()
    {
        // Use the first available business
        $businessId = DB::table('business')->value('id');
        if (!$businessId) {
            $this->command->warn('No business found — skipping DefaultMedicinesSeeder.');
            return;
        }

        // Categories seeded system-wide (business_id = null)
        $cats = [
            'Antibiotics'            => 'Bacterial infection treatment',
            'Antiparasitic'          => 'External and internal parasites',
            'Dewormers'              => 'Intestinal parasites',
            'NSAIDs & Analgesics'    => 'Pain relief and anti-inflammation',
            'Corticosteroids'        => 'Immune suppression and allergy',
            'Vitamins & Supplements' => 'Nutritional support',
            'Gastrointestinal'       => 'Digestive system',
            'Cardiac & Diuretics'    => 'Heart and fluid management',
            'Clinic'            => 'Skin and coat conditions',
            'Ophthalmology'          => 'Eye treatments',
            'Anaesthesia & Sedation' => 'Surgical and procedural sedation',
            'Hormonal'               => 'Reproductive and endocrine',
            'Antifungal'             => 'Fungal infections',
        ];

        $catIds = [];
        foreach ($cats as $name => $desc) {
            $row = DB::table('clc_medicine_categories')
                ->whereNull('business_id')->where('name', $name)->first();

            $catIds[$name] = $row
                ? $row->id
                : DB::table('clc_medicine_categories')->insertGetId([
                    'business_id' => null,
                    'name'        => $name,
                    'description' => $desc,
                    'status'      => 'active',
                    'created_at'  => now(),
                    'updated_at'  => now(),
                ]);
        }

        // [category, name, generic_name, unit, company]
        $medicines = [
            // ── Antibiotics ──────────────────────────────────────────────────────
            ['Antibiotics', 'Amoxicillin',                   'Amoxicillin Trihydrate',        'mg',  'Norbrook / Vétoquinol'],
            ['Antibiotics', 'Amoxicillin + Clavulanate',     'Amoxicillin-Clavulanate',       'mg',  'GSK / Zoetis'],
            ['Antibiotics', 'Enrofloxacin',                  'Enrofloxacin',                  'mg',  'Bayer (Baytril)'],
            ['Antibiotics', 'Metronidazole',                 'Metronidazole',                 'mg',  'Generic'],
            ['Antibiotics', 'Doxycycline',                   'Doxycycline Hyclate',           'mg',  'Generic'],
            ['Antibiotics', 'Tylosin',                       'Tylosin Tartrate',              'mg',  'Elanco'],
            ['Antibiotics', 'Oxytetracycline',               'Oxytetracycline HCl',           'mg',  'Generic'],
            ['Antibiotics', 'Ampicillin',                    'Ampicillin Trihydrate',         'mg',  'Generic'],
            ['Antibiotics', 'Gentamicin',                    'Gentamicin Sulfate',            'mg',  'Generic'],
            ['Antibiotics', 'Clindamycin',                   'Clindamycin HCl',               'mg',  'Pharmacia (Antirobe)'],
            ['Antibiotics', 'Trimethoprim-Sulfamethoxazole', 'TMP-SMX',                       'mg',  'Generic'],
            ['Antibiotics', 'Chloramphenicol',               'Chloramphenicol',               'mg',  'Generic'],

            // ── Antiparasitic ────────────────────────────────────────────────────
            ['Antiparasitic', 'Ivermectin',                  'Ivermectin',                    'ml',  'MSD (Ivomec)'],
            ['Antiparasitic', 'Doramectin',                  'Doramectin',                    'ml',  'Zoetis (Dectomax)'],
            ['Antiparasitic', 'Selamectin',                  'Selamectin',                    'mg',  'Zoetis (Revolution)'],
            ['Antiparasitic', 'Moxidectin',                  'Moxidectin',                    'mg',  'Bayer (Advantage Multi)'],
            ['Antiparasitic', 'Imidacloprid',                'Imidacloprid',                  'mg',  'Bayer (Advantage)'],
            ['Antiparasitic', 'Fipronil',                    'Fipronil',                      'ml',  'Merial (Frontline)'],
            ['Antiparasitic', 'Milbemycin Oxime',            'Milbemycin Oxime',              'mg',  'Elanco (Sentinel)'],

            // ── Dewormers ────────────────────────────────────────────────────────
            ['Dewormers', 'Praziquantel',                    'Praziquantel',                  'mg',  'Bayer (Droncit / Caniquantel)'],
            ['Dewormers', 'Fenbendazole',                    'Fenbendazole',                  'mg',  'MSD (Panacur)'],
            ['Dewormers', 'Albendazole',                     'Albendazole',                   'mg',  'Generic'],
            ['Dewormers', 'Pyrantel Pamoate',                'Pyrantel Pamoate',              'mg',  'Generic'],
            ['Dewormers', 'Toltrazuril',                     'Toltrazuril',                   'ml',  'Bayer (Baycox)'],

            // ── NSAIDs & Analgesics ──────────────────────────────────────────────
            ['NSAIDs & Analgesics', 'Meloxicam',             'Meloxicam',                     'mg',  'Boehringer (Metacam)'],
            ['NSAIDs & Analgesics', 'Carprofen',             'Carprofen',                     'mg',  'Zoetis (Rimadyl)'],
            ['NSAIDs & Analgesics', 'Ketoprofen',            'Ketoprofen',                    'mg',  'Merial (Ketofen)'],
            ['NSAIDs & Analgesics', 'Flunixin Meglumine',    'Flunixin Meglumine',            'mg',  'MSD (Finadyne)'],
            ['NSAIDs & Analgesics', 'Tramadol',              'Tramadol HCl',                  'mg',  'Generic'],
            ['NSAIDs & Analgesics', 'Butorphanol',           'Butorphanol Tartrate',          'mg',  'Zoetis (Torbugesic)'],

            // ── Corticosteroids ──────────────────────────────────────────────────
            ['Corticosteroids', 'Dexamethasone',             'Dexamethasone Sodium Phosphate','mg',  'Generic'],
            ['Corticosteroids', 'Prednisolone',              'Prednisolone',                  'mg',  'Generic'],
            ['Corticosteroids', 'Methylprednisolone',        'Methylprednisolone',            'mg',  'Pfizer (Depo-Medrol)'],
            ['Corticosteroids', 'Betamethasone',             'Betamethasone',                 'mg',  'Generic'],

            // ── Vitamins & Supplements ───────────────────────────────────────────
            ['Vitamins & Supplements', 'Vitamin B Complex',  'Vitamin B1/B6/B12',             'ml',  'Generic'],
            ['Vitamins & Supplements', 'Vitamin C',          'Ascorbic Acid',                 'mg',  'Generic'],
            ['Vitamins & Supplements', 'Vitamin D3',         'Cholecalciferol',               'IU',  'Generic'],
            ['Vitamins & Supplements', 'Calcium Gluconate',  'Calcium Gluconate',             'ml',  'Generic'],
            ['Vitamins & Supplements', 'Iron Dextran',       'Iron Dextran',                  'ml',  'Generic'],
            ['Vitamins & Supplements', 'Multivitamin Supplement', 'Multi-mineral + vitamins', 'tab', 'Various'],

            // ── Gastrointestinal ─────────────────────────────────────────────────
            ['Gastrointestinal', 'Metoclopramide',           'Metoclopramide HCl',            'mg',  'Generic'],
            ['Gastrointestinal', 'Maropitant',               'Maropitant Citrate',            'mg',  'Zoetis (Cerenia)'],
            ['Gastrointestinal', 'Famotidine',               'Famotidine',                    'mg',  'Generic'],
            ['Gastrointestinal', 'Omeprazole',               'Omeprazole',                    'mg',  'Generic'],
            ['Gastrointestinal', 'Atropine Sulfate',         'Atropine Sulfate',              'mg',  'Generic'],
            ['Gastrointestinal', 'Kaolin-Pectin',            'Kaolin + Pectin',               'ml',  'Generic'],
            ['Gastrointestinal', 'Sucralfate',               'Sucralfate',                    'tab', 'Generic'],

            // ── Cardiac & Diuretics ──────────────────────────────────────────────
            ['Cardiac & Diuretics', 'Furosemide',            'Furosemide',                    'mg',  'Sanofi (Lasix)'],
            ['Cardiac & Diuretics', 'Atenolol',              'Atenolol',                      'mg',  'Generic'],
            ['Cardiac & Diuretics', 'Digoxin',               'Digoxin',                       'mg',  'Generic'],
            ['Cardiac & Diuretics', 'Spironolactone',        'Spironolactone',                'mg',  'Generic'],
            ['Cardiac & Diuretics', 'Pimobendan',            'Pimobendan',                    'mg',  'Boehringer (Vetmedin)'],
            ['Cardiac & Diuretics', 'Enalapril',             'Enalapril Maleate',             'mg',  'Generic'],

            // ── Clinic ──────────────────────────────────────────────────────
            ['Clinic', 'Chlorhexidine Solution',        'Chlorhexidine Gluconate 4%',    'ml',  'Generic'],
            ['Clinic', 'Miconazole Cream',              'Miconazole Nitrate 2%',         'g',   'Generic'],
            ['Clinic', 'Betadine Solution',             'Povidone-Iodine 10%',           'ml',  'Mundipharma'],
            ['Clinic', 'Zinc Oxide Ointment',           'Zinc Oxide',                    'g',   'Generic'],

            // ── Antifungal ───────────────────────────────────────────────────────
            ['Antifungal', 'Ketoconazole',                   'Ketoconazole',                  'mg',  'Generic'],
            ['Antifungal', 'Itraconazole',                   'Itraconazole',                  'mg',  'Generic'],
            ['Antifungal', 'Fluconazole',                    'Fluconazole',                   'mg',  'Generic'],

            // ── Ophthalmology ────────────────────────────────────────────────────
            ['Ophthalmology', 'Tobramycin Eye Drops',        'Tobramycin 0.3%',               'ml',  'Alcon'],
            ['Ophthalmology', 'Chloramphenicol Eye Drops',   'Chloramphenicol 0.5%',          'ml',  'Generic'],
            ['Ophthalmology', 'Atropine Eye Drops',          'Atropine Sulfate 1%',           'ml',  'Generic'],
            ['Ophthalmology', 'Dexamethasone Eye Drops',     'Dexamethasone 0.1%',            'ml',  'Generic'],
            ['Ophthalmology', 'Sodium Hyaluronate Drops',    'Sodium Hyaluronate 0.18%',      'ml',  'Generic'],

            // ── Anaesthesia & Sedation ───────────────────────────────────────────
            ['Anaesthesia & Sedation', 'Ketamine',           'Ketamine HCl',                  'ml',  'Generic'],
            ['Anaesthesia & Sedation', 'Xylazine',           'Xylazine HCl',                  'ml',  'Bayer (Rompun)'],
            ['Anaesthesia & Sedation', 'Propofol',           'Propofol',                      'ml',  'Generic'],
            ['Anaesthesia & Sedation', 'Lidocaine 2%',       'Lidocaine HCl',                 'ml',  'Generic'],
            ['Anaesthesia & Sedation', 'Medetomidine',       'Medetomidine HCl',              'ml',  'Orion (Domitor)'],
            ['Anaesthesia & Sedation', 'Midazolam',          'Midazolam HCl',                 'ml',  'Generic'],

            // ── Hormonal ────────────────────────────────────────────────────────
            ['Hormonal', 'Oxytocin',                         'Oxytocin',                      'IU',  'Generic'],
            ['Hormonal', 'Progesterone',                     'Progesterone',                  'mg',  'Generic'],
            ['Hormonal', 'Insulin (Bovine)',                  'Bovine Insulin',                'IU',  'Generic'],
            ['Hormonal', 'Levothyroxine',                    'Levothyroxine Sodium',          'mcg', 'Generic'],
            ['Hormonal', 'Methimazole',                      'Methimazole',                   'mg',  'Generic'],
        ];

        $now = now();
        foreach ($medicines as [$catName, $name, $generic, $unit, $company]) {
            if (DB::table('clc_medicines')->where('business_id', $businessId)->where('name', $name)->exists()) {
                continue;
            }
            DB::table('clc_medicines')->insert([
                'business_id'  => $businessId,
                'category_id'  => $catIds[$catName] ?? null,
                'name'         => $name,
                'generic_name' => $generic,
                'company'      => $company,
                'unit'         => $unit,
                'product_id'   => null,
                'description'  => null,
                'status'       => 'active',
                'created_at'   => $now,
                'updated_at'   => $now,
            ]);
        }
    }
}
