<?php

declare(strict_types=1);

namespace App\Models;

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

class StockOpnameItem extends Model
{
    protected $fillable = [
        'stock_opname_id',
        'item_type',
        'item_id',
        'system_qty',
        'actual_qty',
        'difference_qty',
    ];

    protected $casts = [
        'system_qty' => 'float',
        'actual_qty' => 'float',
        'difference_qty' => 'float',
    ];

    public function stockOpname(): BelongsTo
    {
        return $this->belongsTo(StockOpname::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);
    }
}
