<?php

namespace App\Models;

use App\Models\Concerns\BelongsToOwner;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;

class Patient extends Model
{
    use BelongsToOwner, SoftDeletes;

    protected $table = 'care_patients';

    protected $fillable = [
        'uuid', 'owner_ref', 'organization_id', 'branch_id', 'patient_number',
        'first_name', 'last_name', 'other_names', 'gender', 'date_of_birth',
        'phone', 'email', 'national_id', 'address', 'city', 'region', 'notes',
    ];

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

    protected static function booted(): void
    {
        static::creating(function (Patient $patient) {
            if (! $patient->uuid) {
                $patient->uuid = (string) Str::uuid();
            }
        });
    }

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

    public function fullName(): string
    {
        return trim(implode(' ', array_filter([
            $this->first_name,
            $this->other_names,
            $this->last_name,
        ])));
    }

    public function organization(): BelongsTo
    {
        return $this->belongsTo(Organization::class, 'organization_id');
    }

    public function branch(): BelongsTo
    {
        return $this->belongsTo(Branch::class, 'branch_id');
    }

    public function allergies(): HasMany
    {
        return $this->hasMany(PatientAllergy::class, 'patient_id');
    }

    public function conditions(): HasMany
    {
        return $this->hasMany(PatientCondition::class, 'patient_id');
    }

    public function familyHistory(): HasMany
    {
        return $this->hasMany(PatientFamilyHistory::class, 'patient_id');
    }

    public function emergencyContacts(): HasMany
    {
        return $this->hasMany(EmergencyContact::class, 'patient_id');
    }

    public function insurancePolicies(): HasMany
    {
        return $this->hasMany(InsurancePolicy::class, 'patient_id');
    }

    public function attachments(): HasMany
    {
        return $this->hasMany(PatientAttachment::class, 'patient_id');
    }

    public function appointments(): HasMany
    {
        return $this->hasMany(Appointment::class, 'patient_id');
    }

    public function visits(): HasMany
    {
        return $this->hasMany(Visit::class, 'patient_id');
    }

    public function assessments(): HasMany
    {
        return $this->hasMany(Assessment::class, 'patient_id');
    }

    public function pathways(): HasMany
    {
        return $this->hasMany(PatientPathway::class, 'patient_id');
    }

    public function investigationRequests(): HasMany
    {
        return $this->hasMany(InvestigationRequest::class, 'patient_id');
    }

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

    public function bills(): HasMany
    {
        return $this->hasMany(Bill::class, 'patient_id');
    }
}
