<?php

namespace Hilco\Models;

use Illuminate\Database\Eloquent\Builder;

/**
 * Class DeliveryMethod
 * @package Hilco\Models
 * @property integer $id
 * @property string $delivery_method_code
 * @property string $description
 * @property string $carrier
 * @property bool $is_for_billrcvr
 * @property string $billrcvr_desc
 * @method static \Illuminate\Database\Query\Builder|DeliveryMethod whereId($value)
 * @method static \Illuminate\Database\Query\Builder|DeliveryMethod whereDeliveryMethodCode($value)
 * @method static \Illuminate\Database\Query\Builder|DeliveryMethod whereDescription($value)
 * @method static \Illuminate\Database\Query\Builder|DeliveryMethod whereCarrier($value)
 * @method static Builder isForBillReceiver()
 * @method static Builder forCarriers(...$carriers)
 */
class DeliveryMethod extends WebModel {
    protected $table = "DeliveryMethods";
    protected $connection = 'hilco-live';

    // UPS Supersaver
    const EXPRESSRX_DELIVERY_METHOD = "USS";
    // Purolator
    const PUROLATOR_GROUND = "PRG";
    const PUROLATOR_EXPRESS = "PRX";
    const UPS_2ND_DAY = "U21";
    const UPS_NEXT_DAY_AIR = "U06";

    public function scopeIsForBillReceiver($query) {
        return $query->whereRaw("is_for_billrcvr = 1");
    }

    public function scopeForCarriers ($query, ...$carriers) {
        if (count($carriers) > 1) {
            return $query->whereIn('carrier', $carriers);
        } else if (count($carriers)) {
            return $query->where('carrier', $carriers[0]);
        } else {
            return $query;
        }
    }

    /**
     * @return DeliveryMethod|null
     */
    public static function getExpressRxMethod(): ?DeliveryMethod {
        return DeliveryMethod::where('delivery_method_code', self::EXPRESSRX_DELIVERY_METHOD)->first();
    }

    public function getDeliveryMethodCode(): string {
        return $this->delivery_method_code;
    }

    /**
     * @return string|null
     */
    public function getCarrier(): ?string {
        return $this->carrier;
    }
}