<?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 ClinicalHistory extends Model
{
    use HasFactory, SoftDeletes, BelongsToCompany, HasUuid;

    protected $fillable = [
        'uuid', 'company_id', 'customer_id',
        'history_number', 'opening_date', 'status',
        'allergies', 'diseases', 'current_medications',
        'previous_procedures', 'contraindications', 'skin_sensitivity',
        'special_conditions', 'medical_notes',
        'skin_type', 'hair_type', 'current_color', 'formulas_used',
        'treatments_done', 'initial_diagnosis', 'treatment_evolution',
        'internal_notes',
        'created_by', 'updated_by',
    ];

    /**
     * Campos sensibles cifrados a nivel de aplicación.
     * Quien tenga acceso a la BD no podrá leerlos en claro.
     */
    protected function casts(): array
    {
        return [
            'opening_date'        => 'date',
            'allergies'           => 'encrypted',
            'diseases'            => 'encrypted',
            'current_medications' => 'encrypted',
            'previous_procedures' => 'encrypted',
            'contraindications'   => 'encrypted',
            'skin_sensitivity'    => 'encrypted',
            'special_conditions'  => 'encrypted',
            'medical_notes'       => 'encrypted',
            'formulas_used'       => 'encrypted',
            'initial_diagnosis'   => 'encrypted',
            'treatment_evolution' => 'encrypted',
            'internal_notes'      => 'encrypted',
        ];
    }

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

    public function notes(): HasMany
    {
        return $this->hasMany(ClinicalNote::class)->latest();
    }

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

    public function treatments(): HasMany
    {
        return $this->hasMany(TreatmentRecord::class)->latest('performed_at');
    }

    public function consents(): HasMany
    {
        return $this->hasMany(ConsentForm::class)->latest();
    }

    public function files(): HasMany
    {
        return $this->hasMany(ClientFile::class)->latest();
    }

    public function evolutions(): HasMany
    {
        return $this->hasMany(ClinicalEvolution::class)->latest();
    }

    public function backgrounds(): HasMany
    {
        return $this->hasMany(ClinicalBackground::class)->orderBy('type');
    }

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

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

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

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

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