<?php

namespace App\Models;

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

/**
 * Defines how many grams to deduct from a production batch when a recipe is used.
 *
 * A recipe can have multiple rows here — one per size variant:
 *   - option_set_item_id = NULL  → base / Regular quantity
 *   - option_set_item_id = X     → quantity when size option X is selected
 */
class RecipeProductionItem extends Model
{
    use HasFactory;

    protected $fillable = [
        'recipe_id',
        'source_product_id',
        'grams',
        'option_set_item_id',
        'notes',
    ];

    protected $casts = [
        'grams' => 'decimal:2',
    ];

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

    /**
     * The product whose active production batch will be deducted.
     */
    public function sourceProduct(): BelongsTo
    {
        return $this->belongsTo(Product::class, 'source_product_id');
    }

    /**
     * The size option this row applies to (null = default / Regular).
     */
    public function sizeOption(): BelongsTo
    {
        return $this->belongsTo(OptionSetItem::class, 'option_set_item_id');
    }
}
