<?php

declare(strict_types=1);

namespace App\Models;

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

class StockTransaction extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'outlet_id',
        'user_id',
        'supplier_id',
        'item_type',
        'item_id',
        'qty',
        'type',
        'notes',
    ];

    /**
     * Get the outlet associated with this transaction.
     */
    public function outlet(): BelongsTo
    {
        return $this->belongsTo(Outlet::class);
    }

    /**
     * Get the user who recorded this transaction.
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    /**
     * Get the supplier for this stock addition.
     */
    public function supplier(): BelongsTo
    {
        return $this->belongsTo(Supplier::class);
    }

    /**
     * Get the associated ingredient model when item_type is Ingredient.
     */
    public function ingredient(): BelongsTo
    {
        return $this->belongsTo(Ingredient::class, 'item_id');
    }

    /**
     * Get the parent item details dynamically (Ingredient or Product).
     */
    public function getItemAttribute()
    {
        if ($this->item_type === 'Ingredient') {
            return Ingredient::find($this->item_id);
        }
        return Product::find($this->item_id);
    }
}
