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

    public const STATUS_WAITING = 'waiting';

    public const STATUS_CALLED = 'called';

    public const STATUS_SERVING = 'serving';

    public const STATUS_COMPLETED = 'completed';

    public const STATUS_SKIPPED = 'skipped';

    public const STATUS_CANCELLED = 'cancelled';

    protected $table = 'care_queue_tickets';

    protected $fillable = [
        'uuid', 'owner_ref', 'organization_id', 'service_queue_id', 'service_point_id',
        'ticket_number', 'status', 'priority', 'source', 'customer_name', 'customer_phone',
        'care_entity', 'care_entity_uuid', 'care_entity_id', 'visit_id', 'metadata',
        'called_at', 'serving_at', 'completed_at',
    ];

    protected function casts(): array
    {
        return [
            'metadata' => 'array',
            'called_at' => 'datetime',
            'serving_at' => 'datetime',
            'completed_at' => 'datetime',
            'care_entity_id' => 'integer',
            'visit_id' => 'integer',
        ];
    }

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

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

    public function serviceQueue(): BelongsTo
    {
        return $this->belongsTo(CareServiceQueue::class, 'service_queue_id');
    }

    public function servicePoint(): BelongsTo
    {
        return $this->belongsTo(CareServicePoint::class, 'service_point_id');
    }
}
