<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class OrderItem extends Model
{
    protected $guarded = [];

    protected function casts(): array
    {
        return [
            'fulfillment_payload' => 'array',
        ];
    }

    public function stockCodes()
    {
        return $this->hasMany(StockCode::class, 'sold_order_item_id');
    }

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

    public function productVariant()
    {
        return $this->belongsTo(ProductVariant::class);
    }

    public function providerAttempts()
    {
        return $this->hasMany(ProviderFulfillmentAttempt::class);
    }

    public function latestProviderAttempt()
    {
        return $this->hasOne(ProviderFulfillmentAttempt::class)->latestOfMany();
    }
}
