<?php

namespace Modules\DermatologyClinic\Entities\Drivers;

use Modules\ClinicCore\Entities\Contracts\ClinicSpecialtyDriver;
use Modules\DermatologyClinic\Entities\DermaPatientProfile;

/**
 * Dermatology plugs into ClinicCore as a structural specialty: it contributes
 * a 1:1 patient skin-profile, its own skin-exam sheets, and the Blade
 * fragments ClinicCore renders into the shared patient screens.
 *
 * Registered with ClinicCore's SpecialtyDriverRegistry from the module's
 * service provider boot().
 */
class DermatologySpecialtyDriver implements ClinicSpecialtyDriver
{
    public function key(): string
    {
        return 'dermatology';
    }

    public function label(): string
    {
        return 'Dermatology';
    }

    public function patientProfileModel(): ?string
    {
        return DermaPatientProfile::class;
    }

    public function patientFormFragment(): ?string
    {
        return 'dermaClinic::specialty.patient_form';
    }

    public function patientShowFragment(): ?string
    {
        return 'dermaClinic::specialty.patient_show';
    }

    public function appointmentFragment(): ?string
    {
        // Show the patient's skin profile during the consultation.
        return 'dermaClinic::specialty.patient_show';
    }

    public function persistPatientProfile($patient, array $input): void
    {
        // Nothing dermatology-specific submitted -> don't create an empty row.
        $fitz = $input['fitzpatrick_type'] ?? null;
        $skin = $input['skin_type'] ?? null;
        if (($fitz === null || $fitz === '') && ($skin === null || $skin === '')
            && ! DermaPatientProfile::where('clc_patient_id', $patient->id)->exists()) {
            return;
        }

        DermaPatientProfile::updateOrCreate(
            ['clc_patient_id' => $patient->id],
            ['fitzpatrick_type' => $fitz ?: null, 'skin_type' => $skin ?: null]
        );
    }

    public function examTemplateSeeds(): array
    {
        // Seeded into clc_exam_templates by the plugin's seed migration; kept
        // here as the canonical definition the driver owns.
        return [
            'Dermatological Examination',
            'Skin Cancer Screening (ABCDE)',
            'Acne Severity Assessment',
        ];
    }

    public function terminology(): array
    {
        return [];
    }
}
