<?php

namespace App\Models;

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

class OrderStatusLog extends Model
{
    use HasFactory;

    protected $table = 'tbl_order_status_logs';

    protected $primaryKey = 'id_order_status_log';

    public $incrementing = true;

    protected $keyType = 'int';

    protected $fillable = [
        'order_reference',
        'order_detail_id',
        'status',
        'note',
        'user_id',
        'status_id_old',
        'status_id_new',
        'happened_at',
    ];

    protected function casts(): array
    {
        return [
            'happened_at' => 'datetime',
            'order_detail_id' => 'integer',
            'user_id' => 'integer',
            'status_id_old' => 'integer',
            'status_id_new' => 'integer',
        ];
    }

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

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

    public function orderDetail(): BelongsTo
    {
        return $this->belongsTo(OrderDetail::class, 'order_detail_id', 'id_order_detail');
    }
}
