<?php

namespace Modules\DentalClinic\Entities\Drivers;

use Modules\ClinicCore\Entities\Contracts\ClinicSpecialtyDriver;
use Modules\DentalClinic\Entities\DentalPatientProfile;

/**
 * Dental plugs into ClinicCore as a structural specialty. Unlike Dermatology's
 * flat fields, dental contributes a per-tooth chart (dental_tooth_findings)
 * plus a 1:1 profile, and renders an interactive tooth chart into ClinicCore's
 * shared patient screen.
 */
class DentalSpecialtyDriver implements ClinicSpecialtyDriver
{
    public function key(): string
    {
        return 'dental';
    }

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

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

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

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

    public function appointmentFragment(): ?string
    {
        // During a visit: the interactive tooth chart plus the procedures log
        // (record what was done this appointment). Receives $patient + $appointment.
        return 'dentalClinic::specialty.appointment';
    }

    public function persistPatientProfile($patient, array $input): void
    {
        $dentition = $input['dentition_type'] ?? null;
        $notes     = $input['dental_notes'] ?? null;

        if (($dentition === null || $dentition === '') && ($notes === null || $notes === '')
            && ! DentalPatientProfile::where('clc_patient_id', $patient->id)->exists()) {
            return;
        }

        DentalPatientProfile::updateOrCreate(
            ['clc_patient_id' => $patient->id],
            [
                'dentition_type' => in_array($dentition, ['adult', 'child', 'mixed'], true) ? $dentition : 'adult',
                'notes'          => $notes ?: null,
            ]
        );
    }

    public function examTemplateSeeds(): array
    {
        return ['Dental Examination', 'Periodontal Screening', 'Caries Risk Assessment'];
    }

    public function terminology(): array
    {
        return [
            'caries'    => 'tooth decay',
            'occlusion' => 'bite alignment',
            'gingiva'   => 'gum tissue',
            'pocket'    => 'periodontal pocket depth',
        ];
    }

    /**
     * Optional capability overrides (resolved by ClinicCore's ClinicCapabilities
     * ahead of the clc_specialties row). Dental clinics commonly use sedation /
     * controlled substances and need periodontal charting.
     */
    public function capabilities(): array
    {
        return [
            'controlled_drugs' => true,
            'perio'            => true,
            'vitals'           => true,
            'ai_assist'        => true,
        ];
    }

    /**
     * Optional dental-specific AI guidance folded into the assistant's system
     * prompt (overrides ClcAiController's curated default for 'dental').
     */
    public function aiGuidance(): array
    {
        return [
            'Flag spreading odontogenic infection, airway compromise (Ludwig\'s angina) and trismus as urgent referrals.',
            'Practise antibiotic stewardship — localised pulpitis/periapical disease needs operative care (drainage/extraction/RCT), not routine antibiotics.',
            'Check medical history for bleeding risk, bisphosphonates (MRONJ risk) and infective-endocarditis prophylaxis before invasive procedures.',
            'Use weight-based dosing and reduced LA maximum doses for children.',
        ];
    }
}
