<?php

namespace App\Models\Tenants;

use App\Traits\Auditable;
use App\Traits\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

class ShipmentVehicleDispatch extends Model
{
    use Auditable, BelongsToTenant, SoftDeletes;

    protected $fillable = [
        'tenant_id',
        'shipment_id',
        'seq',
        'logistics_company',
        'arrival_datetime',
        'tonnage',
        'vehicle_no',
        'driver_contact',
        'remarks',
        'options',
    ];

    protected $casts = [
        'seq' => 'integer',
        'shipment_id' => 'integer',
        'arrival_datetime' => 'datetime',
        'options' => 'array',
    ];

    /**
     * 출하 관계
     */
    public function shipment(): BelongsTo
    {
        return $this->belongsTo(Shipment::class);
    }

    /**
     * 다음 순번 가져오기
     */
    public static function getNextSeq(int $shipmentId): int
    {
        $maxSeq = static::where('shipment_id', $shipmentId)->max('seq');

        return ($maxSeq ?? 0) + 1;
    }
}
