<?php

namespace App\Models;

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

class ShippingOrder extends Model
{
    use HasFactory;

    protected $table = 'shipping_orders';

    public const STATUS_NOT_REQUESTED = 'not_requested';

    public const STATUS_PENDING = 'pending';

    public const STATUS_IN_TRANSIT = 'in_transit';

    public const STATUS_DELIVERED = 'delivered';

    public const STATUS_FAILED = 'failed';

    protected $fillable = [
        'orden_lavanderia_id',
        'is_delivery',
        'delivery_address',
        'delivery_status',
        'pending_at',
        'in_transit_at',
        'delivered_at',
        'failed_at',
        'delivered_by_user_id',
    ];

    protected $casts = [
        'is_delivery' => 'boolean',
        'pending_at' => 'datetime',
        'in_transit_at' => 'datetime',
        'delivered_at' => 'datetime',
        'failed_at' => 'datetime',
    ];

    public function ordenLavanderia(): BelongsTo
    {
        return $this->belongsTo(OrdenLavanderia::class, 'orden_lavanderia_id');
    }

    public function deliveredByUser(): BelongsTo
    {
        return $this->belongsTo(User::class, 'delivered_by_user_id');
    }

    /**
     * @param  string|array<int, string>  $status
     */
    public function scopeWithDeliveryStatus($query, $status)
    {
        return is_array($status)
            ? $query->whereIn('delivery_status', $status)
            : $query->where('delivery_status', $status);
    }
}
