<?php

namespace App\Models;

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

class ScoopYield extends Model
{
    use HasFactory;

    protected $fillable = [
        'ingredient_id',
        'option_set_item_id',
        'yield_per_scoop',
        'unit',
        'is_active',
    ];

    protected $casts = [
        'yield_per_scoop' => 'decimal:4',
        'is_active' => 'boolean',
    ];

    public function ingredient(): BelongsTo
    {
        return $this->belongsTo(Ingredient::class);
    }

    public function optionSetItem(): BelongsTo
    {
        return $this->belongsTo(OptionSetItem::class);
    }

    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    public function scopeForIngredient($query, $ingredientId)
    {
        return $query->where('ingredient_id', $ingredientId);
    }

    public function calculateUsage(int $scoopCount): float
    {
        return $this->yield_per_scoop * $scoopCount;
    }
}
