<?php

declare(strict_types=1);

namespace App\Models;

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

class Waste extends Model
{
    protected $fillable = [
        'outlet_id',
        'user_id',
        'item_type',
        'item_id',
        'qty',
        'notes',
    ];

    protected $casts = [
        'qty' => 'float',
    ];

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

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

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

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

    public function getItemAttribute()
    {
        if ($this->item_type === 'Ingredient') {
            return Ingredient::find($this->item_id);
        }
        return Product::find($this->item_id);
    }
}
