<?php

namespace App\Models\Tenant;

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

class PartReservation extends Model
{
    use BelongsToTenant;

    public const STATUS_ACTIVE = 'active';
    public const STATUS_CONSUMED = 'consumed';
    public const STATUS_RELEASED = 'released';

    protected $fillable = [
        'company_id', 'work_order_id', 'work_order_part_id',
        'part_id', 'batch_id', 'qty', 'status',
        'reserved_at', 'consumed_at',
    ];

    protected $casts = [
        'qty' => 'decimal:3',
        'reserved_at' => 'datetime',
        'consumed_at' => 'datetime',
    ];

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

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

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

    public function batch(): BelongsTo
    {
        return $this->belongsTo(PartBatch::class, 'batch_id');
    }

    public function isActive(): bool
    {
        return $this->status === self::STATUS_ACTIVE;
    }
}
