<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class MedicalReferral extends Model
{
    protected $fillable = [
        'medical_record_id', 'doctor_id', 'patient_id',
        'referral_type', 'title', 'items',
        'clinical_notes', 'is_urgent', 'is_printed', 'printed_at',
    ];

    protected $casts = [
        'items'      => 'array',
        'is_urgent'  => 'boolean',
        'is_printed' => 'boolean',
        'printed_at' => 'datetime',
    ];

    public function doctor()        { return $this->belongsTo(Doctor::class); }
    public function patient()       { return $this->belongsTo(Patient::class); }
    public function medicalRecord() { return $this->belongsTo(MedicalRecord::class); }

    // أيقونة ولون حسب النوع
    public static function typeConfig(): array
    {
        return [
            'lab'        => ['label' => 'تحليل مختبري',    'icon' => '🔬', 'color' => '#991b1b', 'bg' => '#fff5f5', 'border' => '#fecaca'],
            'xray'       => ['label' => 'أشعة سينية',       'icon' => '🩻', 'color' => '#1e40af', 'bg' => '#eff6ff', 'border' => '#bfdbfe'],
            'ultrasound' => ['label' => 'سونار',             'icon' => '🫀', 'color' => '#065f46', 'bg' => '#ecfdf5', 'border' => '#a7f3d0'],
            'mri'        => ['label' => 'رنين مغناطيسي',    'icon' => '🧲', 'color' => '#4c1d95', 'bg' => '#f5f3ff', 'border' => '#ddd6fe'],
            'ct'         => ['label' => 'CT Scan',           'icon' => '🖥',  'color' => '#92400e', 'bg' => '#fffbeb', 'border' => '#fde68a'],
            'ecg'        => ['label' => 'تخطيط قلب ECG',    'icon' => '💓', 'color' => '#9d174d', 'bg' => '#fdf2f8', 'border' => '#fbcfe8'],
            'other'      => ['label' => 'إجراء آخر',         'icon' => '📋', 'color' => '#374151', 'bg' => '#f9fafb', 'border' => '#e5e7eb'],
        ];
    }

    public function getTypeConfigAttribute(): array
    {
        return self::typeConfig()[$this->referral_type] ?? self::typeConfig()['other'];
    }
}
