<?php

namespace App\Models;

use App\Enums\ConditionSeverity;
use App\Models\Concerns\BelongsToCompany;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

class ClientCondition extends Model
{
    use HasFactory, SoftDeletes, BelongsToCompany;

    protected $fillable = [
        'company_id', 'clinical_history_id',
        'type', 'name', 'description', 'severity',
        'is_active', 'detected_at',
    ];

    protected function casts(): array
    {
        return [
            'severity'    => ConditionSeverity::class,
            'is_active'   => 'boolean',
            'detected_at' => 'date',
        ];
    }

    public function history(): BelongsTo
    {
        return $this->belongsTo(ClinicalHistory::class, 'clinical_history_id');
    }

    public function scopeActive(Builder $q): Builder
    {
        return $q->where('is_active', true);
    }

    public function scopeCritical(Builder $q): Builder
    {
        return $q->whereIn('severity', ['high', 'critical']);
    }
}
