<?php

namespace App\Models;

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

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

    protected $fillable = [
        'company_id', 'clinical_history_id', 'customer_id',
        'type', 'title', 'description', 'recorded_at', 'created_by',
    ];

    protected function casts(): array
    {
        return [
            'recorded_at' => 'date',
        ];
    }

    public static array $types = [
        'personal'       => 'Personales',
        'family'         => 'Familiares',
        'surgical'       => 'Quirúrgicos',
        'traumatic'      => 'Traumáticos',
        'allergic'       => 'Alérgicos',
        'pharmacological'=> 'Farmacológicos',
        'gynecological'  => 'Gineco-obstétricos',
        'habits'         => 'Hábitos',
        'other'          => 'Otros',
    ];

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

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

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

    public function typeLabel(): string
    {
        return self::$types[$this->type] ?? ucfirst($this->type);
    }
}
