<?php

namespace App\Models\Tenant;

use App\Models\Concerns\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class PartBatch extends Model
{
    use BelongsToTenant;

    protected $fillable = [
        'company_id', 'part_id', 'warehouse_id', 'supplier_id',
        'batch_ref', 'qty_in', 'qty_remaining', 'buy_price',
        'received_at', 'notes',
    ];

    protected $casts = [
        'qty_in' => 'decimal:3',
        'qty_remaining' => 'decimal:3',
        'buy_price' => 'decimal:2',
        'received_at' => 'datetime',
    ];

    public function part(): BelongsTo
    {
        return $this->belongsTo(Part::class);
    }

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

    public function supplier(): BelongsTo
    {
        return $this->belongsTo(Supplier::class);
    }

    public function reservations(): HasMany
    {
        return $this->hasMany(PartReservation::class, 'batch_id');
    }

    public function isDepleted(): bool
    {
        return (float) $this->qty_remaining <= 0;
    }
}
