<?php

namespace App\Models;

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

class OrderShipment extends Model
{
    use HasFactory;

    protected $table = 'tbl_order_shipments';

    protected $primaryKey = 'id_order_shipment';

    public $incrementing = true;

    protected $keyType = 'int';

    protected $fillable = [
        'order_reference',
        'carrier_id',
        'carrier_shipment_id',
        'tracking_number',
        'weight_kg',
        'weight_g',
        'length_cm',
        'width_cm',
        'height_cm',
        'quoted_cost',
        'final_cost',
        'currency',
        'label_base64',
        'label_storage_path',
        'quote_payload',
        'quote_response',
        'create_payload',
        'create_response',
        'status',
        'created_by',
    ];

    protected function casts(): array
    {
        return [
            'weight_kg' => 'decimal:3',
            'quoted_cost' => 'decimal:6',
            'final_cost' => 'decimal:6',
            'quote_payload' => 'array',
            'quote_response' => 'array',
            'create_payload' => 'array',
            'create_response' => 'array',
            'created_by' => 'integer',
        ];
    }

    public function order(): BelongsTo
    {
        return $this->belongsTo(Order::class, 'order_reference', 'order_reference');
    }

    public function carrierCatalog(): BelongsTo
    {
        return $this->belongsTo(OrderCarrierCatalog::class, 'carrier_id', 'id_order_carrier');
    }

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