<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class StockMovement extends Model
{
    protected $fillable = [
        'product_id', 'sku', 'product_name',
        'type', 'reason', 'qty',
        'stock_before', 'stock_after',
        'reference_no', 'user_id', 'warehouse_id', 'notes',
    ];

    protected function casts(): array
    {
        return [
            'qty'          => 'integer',
            'stock_before' => 'integer',
            'stock_after'  => 'integer',
        ];
    }

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

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

    public function warehouse()
    {
        return $this->belongsTo(Warehouse::class);
    }

    // Accessor: user_name
    public function getUserNameAttribute(): string
    {
        return $this->user?->name ?? 'System';
    }
}
