<?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 Illuminate\Database\Eloquent\Relations\HasOne;
use Kylin\Medical\Enums\ConsultationStatus;
use Kylin\Medical\Enums\ConsultationType;

/**
 * 问诊记录模型
 *
 * 管理在线/线下问诊记录，包括问诊类型、状态、诊断结果等。
 *
 * @property string $consultation_type 问诊类型
 * @property string $status            问诊状态
 * @property string $diagnosis         诊断结果
 */
class Consultation extends Model
{
    protected $table = 'consultations';

    protected $fillable = [
        'appointment_id',
        'patient_id',
        'doctor_id',
        'type',
        'status',
        'started_at',
        'ended_at',
        'transfer_to',
        'transfer_reason',
    ];

    protected $casts = [
        'type' => ConsultationType::class,
        'status' => ConsultationStatus::class,
        'started_at' => 'datetime',
        'ended_at' => 'datetime',
    ];

    /**
     * 关联挂号
     */
    public function appointment(): BelongsTo
    {
        return $this->belongsTo(Appointment::class);
    }

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

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

    /**
     * 问诊消息
     */
    public function messages(): HasMany
    {
        return $this->hasMany(Message::class);
    }

    /**
     * 电子病历
     */
    public function medicalRecord(): HasOne
    {
        return $this->hasOne(MedicalRecord::class);
    }
}
