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

    public const STATUS_DRAFT = 'draft';

    public const STATUS_ORDERED = 'ordered';

    public const STATUS_SENT = 'sent';

    public const STATUS_RECEIVED = 'received';

    public const STATUS_FITTED = 'fitted';

    public const STATUS_CANCELLED = 'cancelled';

    protected $table = 'care_dental_lab_cases';

    protected $fillable = [
        'uuid', 'owner_ref', 'organization_id', 'patient_id', 'visit_id', 'plan_item_id',
        'tooth_codes', 'case_type', 'lab_name', 'status',
        'ordered_at', 'due_at', 'received_at', 'notes', 'created_by',
    ];

    protected function casts(): array
    {
        return [
            'tooth_codes' => 'array',
            'ordered_at' => 'datetime',
            'due_at' => 'date',
            'received_at' => 'datetime',
        ];
    }

    protected static function booted(): void
    {
        static::creating(function (DentalLabCase $case) {
            if (! $case->uuid) {
                $case->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 planItem(): BelongsTo
    {
        return $this->belongsTo(DentalPlanItem::class, 'plan_item_id');
    }
}
