<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Str;

class DentalPlanItem extends Model
{
    public const STATUS_PROPOSED = 'proposed';

    public const STATUS_ACCEPTED = 'accepted';

    public const STATUS_DONE = 'done';

    public const STATUS_CANCELLED = 'cancelled';

    protected $table = 'care_dental_plan_items';

    protected $fillable = [
        'uuid', 'treatment_plan_id', 'tooth_code', 'surfaces', 'procedure_code',
        'label', 'priority', 'status', 'estimate_minor', 'bill_line_item_id', 'notes',
    ];

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

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

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

    public function plan(): BelongsTo
    {
        return $this->belongsTo(DentalTreatmentPlan::class, 'treatment_plan_id');
    }

    public function billLineItem(): BelongsTo
    {
        return $this->belongsTo(BillLineItem::class, 'bill_line_item_id');
    }
}
