<?php

namespace Mtc\Plugins\Consumables\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Mtc\Shop\Item;

class ItemConsumable extends Model
{
    public const DEFAULT_LOW_STOCK_THRESHOLD = 10;

    public static function lowStockThreshold(): int
    {
        return defined('CONSUMABLES_LOW_STOCK_THRESHOLD')
            ? (int) CONSUMABLES_LOW_STOCK_THRESHOLD
            : self::DEFAULT_LOW_STOCK_THRESHOLD;
    }

    protected $table = 'items_consumables';

    protected $fillable = [
        'item_id',
        'consumable_item_id',
        'quantity',
    ];

    protected $casts = [
        'quantity' => 'integer',
    ];

    public function item(): BelongsTo
    {
        return $this->belongsTo(Item::class, 'item_id');
    }

    public function consumableItem(): BelongsTo
    {
        return $this->belongsTo(Item::class, 'consumable_item_id');
    }
}
