<?php

namespace App\Models;

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

class BatchSizeConfig extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'size_code',
        'size_value',
        'size_unit',
        'scoops_per_batch',
        'ingredient_multiplier',
        'default_expiry_hours',
        'is_active',
    ];

    protected $casts = [
        'size_value' => 'decimal:2',
        'scoops_per_batch' => 'integer',
        'ingredient_multiplier' => 'decimal:4',
        'default_expiry_hours' => 'integer',
        'is_active' => 'boolean',
    ];

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

    public function scopeByCode($query, string $code)
    {
        return $query->where('size_code', $code);
    }

    public static function findByCode(string $code): ?self
    {
        return static::where('size_code', $code)->first();
    }

    public function getExpiryDateTime(): \Carbon\Carbon
    {
        return now()->addHours($this->default_expiry_hours);
    }
}
