<?php

namespace Modules\MedRegistry\Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Schema;
use Modules\MedRegistry\Entities\MedregBusinessType;

/**
 * P1-T6 — the 10 business-type presets + Custom, with default/optional module
 * lists (as spec_keys) EXACTLY per SPEC-INDEX §1 / SIFA-MEDICAL-SPECS
 * "Business Types & Module Presets". Idempotent upsert on type_key.
 */
class MedregBusinessTypesSeeder extends Seeder
{
    public function run(): void
    {
        if (! Schema::hasTable('medreg_business_types')) {
            return;
        }

        $sort = 0;

        foreach ($this->types() as $type) {
            $type['sort_order'] = $sort += 10;
            $type['is_active'] = true;

            MedregBusinessType::updateOrCreate(
                ['type_key' => $type['type_key']],
                $type
            );
        }
    }

    /**
     * Preset data (labels/descriptions from the spec yaml; the hospital
     * label/description block is garbled in the source spec — closest sensible
     * wording used, flagged per plan Ground Rule 6).
     */
    protected function types(): array
    {
        return [
            [
                'type_key'        => 'hospital',
                'label'           => 'Hospital',
                'description'     => 'Full inpatient + outpatient hospital',
                'default_modules' => [
                    'medical_core', 'scheduling', 'inpatient', 'outpatient', 'emergency',
                    'surgery', 'pharmacy', 'laboratory', 'radiology', 'bed_management',
                    'billing', 'inventory', 'hr_payroll', 'accounting', 'ambulance',
                    'icu', 'blood_bank', 'nicu', 'dialysis', 'physiotherapy',
                    'mortuary', 'all_specialty_modules',
                ],
                'optional_modules' => [],
            ],
            [
                'type_key'        => 'polyclinic',
                'label'           => 'Polyclinic / Medical Center',
                'description'     => 'Multi-specialty outpatient facility',
                'default_modules' => [
                    'medical_core', 'scheduling', 'outpatient', 'pharmacy',
                    'laboratory', 'billing', 'inventory',
                ],
                'optional_modules' => ['radiology', 'all_specialty_modules'],
            ],
            [
                'type_key'        => 'single_clinic',
                'label'           => 'Single Specialty Clinic',
                'description'     => 'One-specialty outpatient clinic (e.g., dental, derma, ophthalmology)',
                'default_modules' => ['medical_core', 'scheduling', 'outpatient', 'billing'],
                'optional_modules' => ['pharmacy', 'one_specialty_module_of_choice'],
            ],
            [
                'type_key'        => 'bioclinic',
                'label'           => 'BioClinic (Multi-Clinic Brand)',
                'description'     => 'Multiple clinics under one brand, possibly different specialties per location. '
                    . 'Each facility (branch) enables its own specialty modules.',
                'default_modules' => ['medical_core', 'scheduling', 'outpatient', 'billing', 'inventory'],
                'optional_modules' => ['pharmacy', 'laboratory', 'all_specialty_modules'],
            ],
            [
                'type_key'        => 'laboratory',
                'label'           => 'Laboratory / Pathology Lab',
                'description'     => 'Standalone lab accepting samples from external providers',
                'default_modules' => ['medical_core', 'laboratory', 'billing', 'inventory'],
                'optional_modules' => ['specimen_tracking', 'outreach_management'],
            ],
            [
                'type_key'        => 'imaging_center',
                'label'           => 'Imaging / Radiology Center',
                'description'     => 'Standalone diagnostic imaging facility',
                'default_modules' => ['medical_core', 'radiology', 'scheduling', 'billing'],
                'optional_modules' => ['teleradiology'],
            ],
            [
                'type_key'        => 'pharmacy',
                'label'           => 'Pharmacy',
                'description'     => 'Retail or institutional pharmacy',
                'default_modules' => ['medical_core', 'pharmacy', 'billing', 'inventory'],
                'optional_modules' => ['drug_interaction_engine', 'insurance_claims'],
            ],
            [
                'type_key'        => 'dialysis_center',
                'label'           => 'Dialysis Center',
                'description'     => 'Standalone dialysis facility',
                'default_modules' => ['medical_core', 'scheduling', 'dialysis', 'billing', 'inventory'],
                'optional_modules' => [],
            ],
            [
                'type_key'        => 'physiotherapy_center',
                'label'           => 'Physiotherapy / Rehab Center',
                'description'     => 'Standalone rehabilitation facility',
                'default_modules' => ['medical_core', 'scheduling', 'physiotherapy', 'billing'],
                'optional_modules' => [],
            ],
            [
                'type_key'        => 'diagnostic_center',
                'label'           => 'Comprehensive Diagnostic Center',
                'description'     => 'Combined lab + imaging + examination facility',
                'default_modules' => [
                    'medical_core', 'laboratory', 'radiology', 'scheduling', 'billing', 'inventory',
                ],
                'optional_modules' => [
                    'ecg_echo', 'endoscopy', 'pulmonary_function', 'audiometry', 'vision_testing',
                ],
            ],
            [
                'type_key'        => 'custom',
                'label'           => 'Custom',
                'description'     => 'Manual module selection from the full medical catalog',
                'default_modules' => [],
                'optional_modules' => [],
            ],
        ];
    }
}
