<?php

namespace App\Models;

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

class StockHistory extends Model
{
    protected $fillable = [
        'product_id',
        'adjustment_type',
        'quantity',
        'old_stock',
        'new_stock',
        'reason',
        'reference_id',
        'reference_type',
        'adjusted_by'
    ];
    
    /**
     * Get the product associated with the stock history.
     */
    public function product(): BelongsTo
    {
        return $this->belongsTo(Product::class);
    }
    
    /**
     * Get the user who made the adjustment.
     */
    public function adjuster(): BelongsTo
    {
        return $this->belongsTo(User::class, 'adjusted_by');
    }
    
    /**
     * Get the adjustment type in human readable format.
     */
    public function getAdjustmentTypeTextAttribute()
    {
        return $this->adjustment_type == 'add' ? 'Stock Added' : 'Stock Removed';
    }
    
    /**
     * Get the color based on adjustment type.
     */
    public function getAdjustmentColorAttribute()
    {
        return $this->adjustment_type == 'add' ? 'success' : 'danger';
    }
    
    /**
     * Get the icon based on adjustment type.
     */
    public function getAdjustmentIconAttribute()
    {
        return $this->adjustment_type == 'add' ? 'plus-circle' : 'minus-circle';
    }
}