<?php

namespace App\Models;

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

class DentalRecall extends Model
{
    use BelongsToOwner;

    public const STATUS_SCHEDULED = 'scheduled';

    public const STATUS_DUE = 'due';

    public const STATUS_COMPLETED = 'completed';

    public const STATUS_CANCELLED = 'cancelled';

    protected $table = 'care_dental_recalls';

    protected $fillable = [
        'uuid', 'owner_ref', 'organization_id', 'patient_id',
        'due_on', 'reason', 'status', 'linked_visit_id', 'notes', 'created_by',
    ];

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

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

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

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

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