<?php

namespace Modules\ClinicLab\Entities;

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

/**
 * An external-patient sample logged from an outreach provider. Runs through the
 * same specimen flow (specimen_id) then is delivered over a channel.
 */
class CllOutreachOrder extends Model
{
    protected $table = 'cll_outreach_orders';

    protected $fillable = [
        'business_id', 'provider_id', 'patient_name', 'specimen_id', 'catalog_id',
        'status', 'received_at', 'reported_at', 'delivery_channel',
    ];

    protected $casts = [
        'received_at' => 'datetime',
        'reported_at' => 'datetime',
    ];

    public const STATUSES = [
        'received'   => 'Received',
        'processing' => 'Processing',
        'resulted'   => 'Resulted',
        'reported'   => 'Reported',
        'delivered'  => 'Delivered',
    ];

    public const CHANNELS = [
        'portal'   => 'Portal',
        'email'    => 'Email',
        'print'    => 'Print',
        'whatsapp' => 'WhatsApp',
    ];

    public function provider(): BelongsTo
    {
        return $this->belongsTo(CllOutreachProvider::class, 'provider_id');
    }

    public function specimen(): BelongsTo
    {
        return $this->belongsTo(CllSpecimen::class, 'specimen_id');
    }

    public function catalog(): BelongsTo
    {
        return $this->belongsTo(CllTestCatalog::class, 'catalog_id');
    }

    public function scopeForBusiness($query, $businessId)
    {
        return $query->where('business_id', $businessId);
    }
}
