<?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\Support\Str;

class DentalPerioExam extends Model
{
    use BelongsToOwner;

    protected $table = 'care_dental_perio_exams';

    protected $fillable = [
        'uuid', 'owner_ref', 'organization_id', 'patient_id', 'visit_id',
        'exam_date', 'examiner', 'notes',
    ];

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

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

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

    public function patient(): BelongsTo
    {
        return $this->belongsTo(Patient::class, 'patient_id');
    }

    public function visit(): BelongsTo
    {
        return $this->belongsTo(Visit::class, 'visit_id');
    }

    public function sites(): HasMany
    {
        return $this->hasMany(DentalPerioSite::class, 'perio_exam_id');
    }
}
