<?php

namespace Modules\DermatologyClinic\Entities;

use Illuminate\Database\Eloquent\Model;
use Modules\ClinicCore\Entities\ClcPatient;

/**
 * One recorded skin lesion on a shared ClinicCore patient (1:many). Drives the
 * body-map editor, ABCDE skin-cancer checklist and per-lesion photo comparison.
 *
 * Photos are NOT owned here — photo_ids references rows in clc_clinical_photos
 * (the shared ClinicCore before/after photography feature), so lesion images
 * ride on the same upload/storage pipeline as every other clinic photo.
 */
class DermaLesion extends Model
{
    protected $table = 'derma_lesions';
    protected $guarded = ['id'];

    protected $casts = [
        'abcde_flags'  => 'array',
        'photo_ids'    => 'array',
        'biopsy_taken' => 'boolean',
        'size_mm'      => 'float',
        'position_x'   => 'integer',
        'position_y'   => 'integer',
    ];

    /** Body regions matching the derma_lesions.body_region enum + the SVG body map. */
    public const BODY_REGIONS = [
        'head'       => 'Head',
        'face'       => 'Face',
        'neck'       => 'Neck',
        'chest'      => 'Chest',
        'back'       => 'Back',
        'abdomen'    => 'Abdomen',
        'left_arm'   => 'Left arm',
        'right_arm'  => 'Right arm',
        'left_hand'  => 'Left hand',
        'right_hand' => 'Right hand',
        'left_leg'   => 'Left leg',
        'right_leg'  => 'Right leg',
        'left_foot'  => 'Left foot',
        'right_foot' => 'Right foot',
        'genital'    => 'Genital',
        'other'      => 'Other',
    ];

    /** Morphology enum values. */
    public const MORPHOLOGIES = [
        'macule'  => 'Macule',
        'patch'   => 'Patch',
        'papule'  => 'Papule',
        'plaque'  => 'Plaque',
        'nodule'  => 'Nodule',
        'tumor'   => 'Tumor',
        'vesicle' => 'Vesicle',
        'bulla'   => 'Bulla',
        'pustule' => 'Pustule',
        'wheal'   => 'Wheal',
        'scar'    => 'Scar',
        'ulcer'   => 'Ulcer',
        'other'   => 'Other',
    ];

    public const BORDERS = [
        'well_defined' => 'Well defined',
        'ill_defined'  => 'Ill defined',
        'irregular'    => 'Irregular',
    ];

    public const EVOLUTIONS = [
        'stable'    => 'Stable',
        'growing'   => 'Growing',
        'shrinking' => 'Shrinking',
        'new'       => 'New',
        'recurrent' => 'Recurrent',
    ];

    public const BIOPSY_TYPES = [
        'shave'      => 'Shave',
        'punch'      => 'Punch',
        'excisional' => 'Excisional',
        'incisional' => 'Incisional',
    ];

    public const STATUSES = [
        'active'     => 'Active',
        'resolved'   => 'Resolved',
        'excised'    => 'Excised',
        'monitoring' => 'Monitoring',
    ];

    /** ABCDE checklist keys (Asymmetry, Border, Color, Diameter, Evolving). */
    public const ABCDE_KEYS = [
        'asymmetry' => 'Asymmetry',
        'border'    => 'Border irregularity',
        'color'     => 'Colour variation',
        'diameter'  => 'Diameter > 6 mm',
        'evolving'  => 'Evolving',
    ];

    public function patient()
    {
        return $this->belongsTo(ClcPatient::class, 'patient_id');
    }

    /** Number of ABCDE flags currently set. */
    public function abcdeCount(): int
    {
        $flags = $this->abcde_flags ?: [];
        return count(array_filter($flags, fn ($v) => (bool) $v));
    }

    /** 3+ ABCDE flags → suggest a biopsy (skin-cancer screening hint). */
    public function suggestsBiopsy(): bool
    {
        return $this->abcdeCount() >= 3 && ! $this->biopsy_taken;
    }
}
