<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class InventoryBalance extends Model
{
    protected $fillable = [
        'location_id',
        'product_id',
        'on_hand',
        'reserved',
        'damaged',
        'in_transit_in',
        'in_transit_out',
    ];

    protected function casts(): array
    {
        return [
            'on_hand' => 'integer',
            'reserved' => 'integer',
            'damaged' => 'integer',
            'in_transit_in' => 'integer',
            'in_transit_out' => 'integer',
        ];
    }

    public function location(): BelongsTo
    {
        return $this->belongsTo(Location::class);
    }

    public function product(): BelongsTo
    {
        return $this->belongsTo(Product::class);
    }

    public function available(): int
    {
        return max(0, $this->on_hand - $this->reserved - $this->damaged);
    }

    public function getAvailableAttribute(): int
    {
        return $this->available();
    }
}
