<?php

namespace App\Models;

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

class StockHistory extends Model
{
    use HasFactory;

    protected $table = 'tbl_stock_histories';

    protected $fillable = [
        'product_id',
        'user_id',
        'type',
        'source',
        'quantity',
        'stock_before',
        'stock_after',
        'reference_id',
        'reference_type',
        'note',
    ];

    // =====================
    // RELATIONSHIPS
    // =====================

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

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

    // Optional polymorphic (kalau mau future proof)
    public function reference()
    {
        return $this->morphTo(null, 'reference_type', 'reference_id');
    }
}