<?php

namespace App\Models;

use App\Models\Concerns\BelongsToCompany;
use App\Models\Concerns\HasUuid;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class ClinicalEvolution extends Model
{
    use HasFactory, SoftDeletes, BelongsToCompany, HasUuid;

    protected $fillable = [
        'uuid', 'company_id', 'clinical_history_id', 'customer_id',
        'booking_id', 'employee_id',
        'consultation_reason', 'current_illness', 'physical_exam',
        'vital_signs', 'diagnosis_notes', 'treatment_plan', 'recommendations',
        'next_control_date', 'status', 'signed_at', 'signed_by',
        'created_by', 'updated_by',
    ];

    protected function casts(): array
    {
        return [
            'vital_signs'       => 'array',
            'next_control_date' => 'date',
            'signed_at'         => 'datetime',
        ];
    }

    public function clinicalHistory(): BelongsTo
    {
        return $this->belongsTo(ClinicalHistory::class);
    }

    public function customer(): BelongsTo
    {
        return $this->belongsTo(Customer::class);
    }

    public function employee(): BelongsTo
    {
        return $this->belongsTo(Employee::class);
    }

    public function booking(): BelongsTo
    {
        return $this->belongsTo(Booking::class);
    }

    public function diagnoses(): HasMany
    {
        return $this->hasMany(ClinicalDiagnosis::class);
    }

    public function prescriptions(): HasMany
    {
        return $this->hasMany(Prescription::class);
    }

    public function orders(): HasMany
    {
        return $this->hasMany(ClinicalOrder::class);
    }

    public function signer(): BelongsTo
    {
        return $this->belongsTo(User::class, 'signed_by');
    }

    public function creator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    public function isSigned(): bool
    {
        return $this->status === 'signed';
    }

    public function isDraft(): bool
    {
        return $this->status === 'draft';
    }

    public function getRouteKeyName(): string
    {
        return 'uuid';
    }
}
