<?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 InvestigationRequest extends Model
{
    use BelongsToOwner, SoftDeletes;

    public const STATUS_PENDING = 'pending';

    public const STATUS_SAMPLE_COLLECTED = 'sample_collected';

    public const STATUS_IN_PROGRESS = 'in_progress';

    public const STATUS_AWAITING_REVIEW = 'awaiting_review';

    public const STATUS_COMPLETED = 'completed';

    public const STATUS_DELIVERED = 'delivered';

    public const STATUS_CANCELLED = 'cancelled';

    protected $table = 'care_investigation_requests';

    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', 'visit_id', 'consultation_id',
        'patient_id', 'investigation_type_id', 'practitioner_id', 'status', 'priority',
        'clinical_notes', 'requested_by', 'assigned_member_id', 'sample_barcode',
        'sample_collected_at', 'sample_collected_by', 'completed_at', 'delivered_at',
        'approved_by', 'approved_at',
    ];

    protected function casts(): array
    {
        return [
            'sample_collected_at' => 'datetime',
            'completed_at' => 'datetime',
            'delivered_at' => 'datetime',
            'approved_at' => 'datetime',
        ];
    }

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

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

    public function investigationType(): BelongsTo
    {
        return $this->belongsTo(InvestigationType::class, 'investigation_type_id');
    }

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

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

    public function consultation(): BelongsTo
    {
        return $this->belongsTo(Consultation::class, 'consultation_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 assignedMember(): BelongsTo
    {
        return $this->belongsTo(Member::class, 'assigned_member_id');
    }

    public function result(): HasOne
    {
        return $this->hasOne(InvestigationResult::class, 'investigation_request_id');
    }

    /** @return list<string> */
    public static function activeStatuses(): array
    {
        return [
            self::STATUS_PENDING,
            self::STATUS_SAMPLE_COLLECTED,
            self::STATUS_IN_PROGRESS,
            self::STATUS_AWAITING_REVIEW,
            self::STATUS_COMPLETED,
        ];
    }
}
