<?php

declare(strict_types=1);

namespace Kylin\Medical\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Kylin\Medical\Enums\MedicalRecordStatus;

/**
 * 病历模型
 *
 * 管理患者电子病历，包括主诉、现病史、既往史、诊断等。
 *
 * @property string $chief_complaint  主诉
 * @property string $present_illness  现病史
 * @property string $past_history     既往史
 * @property string $diagnosis        诊断
 * @property string $status           状态
 */
class MedicalRecord extends Model
{
    protected $table = 'medical_records';

    protected $fillable = [
        'consultation_id',
        'patient_id',
        'doctor_id',
        'chief_complaint',
        'present_illness',
        'past_history',
        'physical_exam',
        'diagnosis_code',
        'diagnosis_name',
        'treatment_plan',
        'visit_date',
        'department',
        'status',
        'signed_at',
    ];

    protected $casts = [
        'status' => MedicalRecordStatus::class,
        'visit_date' => 'date',
        'signed_at' => 'datetime',
    ];

    /**
     * 关联问诊
     */
    public function consultation(): BelongsTo
    {
        return $this->belongsTo(Consultation::class);
    }

    /**
     * 关联患者
     */
    public function patient(): BelongsTo
    {
        return $this->belongsTo(Patient::class);
    }

    /**
     * 关联医生
     */
    public function doctor(): BelongsTo
    {
        return $this->belongsTo(Doctor::class);
    }

    /**
     * 处方列表
     */
    public function prescriptions(): HasMany
    {
        return $this->hasMany(Prescription::class);
    }
}
