<?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\PrescriptionStatus;
use Kylin\Medical\Enums\PrescriptionType;

/**
 * 处方模型
 *
 * 管理医生开具的处方，包括处方类型、状态、药品明细等。
 *
 * @property string $prescription_type 处方类型
 * @property string $status            处方状态
 * @property string $diagnosis         诊断
 */
class Prescription extends Model
{
    protected $table = 'prescriptions';

    protected $fillable = [
        'medical_record_id',
        'doctor_id',
        'pharmacist_id',
        'status',
        'type',
        'valid_until',
        'reject_reason',
        'reviewed_at',
    ];

    protected $casts = [
        'status' => PrescriptionStatus::class,
        'type' => PrescriptionType::class,
        'valid_until' => 'date',
        'reviewed_at' => 'datetime',
    ];

    /**
     * 关联病历
     */
    public function medicalRecord(): BelongsTo
    {
        return $this->belongsTo(MedicalRecord::class);
    }

    /**
     * 开方医师
     */
    public function doctor(): BelongsTo
    {
        return $this->belongsTo(Doctor::class);
    }

    /**
     * 审核药师
     */
    public function pharmacist(): BelongsTo
    {
        return $this->belongsTo(Doctor::class, 'pharmacist_id');
    }

    /**
     * 处方明细
     */
    public function items(): HasMany
    {
        return $this->hasMany(PrescriptionItem::class);
    }
}
