<?php

namespace Modules\ClinicCore\Entities;

use Illuminate\Database\Eloquent\Model;

/**
 * SIFA P3-T8 — a patient-safety alert. Rendered by the shared alert banner
 * partial (cliniccore::partials.patient_alert_banner) on any clinical screen.
 */
class ClcMedicalAlert extends Model
{
    protected $table = 'clc_medical_alerts';
    protected $guarded = ['id'];

    protected $casts = [
        'is_active'        => 'boolean',
        'acknowledged_at'  => 'datetime',
    ];

    public const TYPES = [
        'allergy', 'drug_interaction', 'duplicate_therapy', 'critical_value',
        'fall_risk', 'isolation', 'consent_missing', 'vte_risk', 'other',
    ];
    public const SEVERITIES = ['info', 'warning', 'critical'];

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

    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    /** Bootstrap contextual colour for the banner chip. */
    public function bannerClass(): string
    {
        return [
            'critical' => 'danger',
            'warning'  => 'warning',
            'info'     => 'info',
        ][$this->severity] ?? 'warning';
    }

    public function icon(): string
    {
        return [
            'allergy'           => 'fa-allergies',
            'drug_interaction'  => 'fa-pills',
            'duplicate_therapy' => 'fa-clone',
            'critical_value'    => 'fa-heartbeat',
            'fall_risk'         => 'fa-person-falling',
            'isolation'         => 'fa-head-side-mask',
            'consent_missing'   => 'fa-file-signature',
            'vte_risk'          => 'fa-notes-medical',
        ][$this->alert_type] ?? 'fa-triangle-exclamation';
    }
}
