<?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\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;

class Appointment extends Model
{
    use BelongsToOwner, SoftDeletes;

    public const STATUS_SCHEDULED = 'scheduled';

    public const STATUS_CHECKED_IN = 'checked_in';

    public const STATUS_WAITING = 'waiting';

    public const STATUS_IN_CONSULTATION = 'in_consultation';

    public const STATUS_COMPLETED = 'completed';

    public const STATUS_CANCELLED = 'cancelled';

    public const STATUS_NO_SHOW = 'no_show';

    public const TYPE_SCHEDULED = 'scheduled';

    public const TYPE_WALK_IN = 'walk_in';

    protected $table = 'care_appointments';

    protected $fillable = [
        'uuid', 'queue_ticket_uuid', 'queue_ticket_number', 'queue_ticket_status',
        'queue_service_point_uuid', 'queue_destination', 'queue_staff_display_name', 'queue_routing_status',
        'owner_ref', 'organization_id', 'branch_id', 'patient_id',
        'practitioner_id', 'department_id', 'visit_id', 'type', 'status',
        'scheduled_at', 'checked_in_at', 'waiting_at', 'started_at',
        'completed_at', 'cancelled_at', 'queue_position', 'reason', 'notes',
        'meet_room_uuid', 'meet_join_url', 'created_by',
    ];

    protected function casts(): array
    {
        return [
            'scheduled_at' => 'datetime',
            'checked_in_at' => 'datetime',
            'waiting_at' => 'datetime',
            'started_at' => 'datetime',
            'completed_at' => 'datetime',
            'cancelled_at' => 'datetime',
        ];
    }

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

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

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

    public function practitioner(): BelongsTo
    {
        return $this->belongsTo(Practitioner::class, 'practitioner_id');
    }

    public function branch(): BelongsTo
    {
        return $this->belongsTo(Branch::class, 'branch_id');
    }

    public function department(): BelongsTo
    {
        return $this->belongsTo(Department::class, 'department_id');
    }

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

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

    public function consultation(): HasOne
    {
        return $this->hasOne(Consultation::class, 'appointment_id')->latestOfMany();
    }

    /** @return list<string> */
    public static function activeStatuses(): array
    {
        return [
            self::STATUS_SCHEDULED,
            self::STATUS_CHECKED_IN,
            self::STATUS_WAITING,
            self::STATUS_IN_CONSULTATION,
        ];
    }
}
