<?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 DentalChart extends Model
{
    use BelongsToOwner;

    protected $table = 'care_dental_charts';

    protected $fillable = [
        'uuid', 'owner_ref', 'organization_id', 'patient_id',
        'notation', 'dentition', 'charted_at', 'charted_by',
    ];

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

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

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

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

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

    public function teeth(): HasMany
    {
        return $this->hasMany(DentalTooth::class, 'dental_chart_id');
    }

    public function events(): HasMany
    {
        return $this->hasMany(DentalChartEvent::class, 'dental_chart_id');
    }
}
