<?php

declare(strict_types=1);

namespace App\Models;

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

class DberpInventoryBatch extends Model
{
    use HasDberpAuditTrail;

    protected $table = 'dberp_inventory_batches';

    /**
     * @var list<string>
     */
    protected $fillable = [
        'product_id',
        'warehouse_id',
        'batch_no',
        'source_system',
        'source_document_type',
        'source_document_id',
        'source_document_no',
        'external_reference_id',
        'production_date',
        'expiry_date',
        'shelf_life_days',
        'realtime_quantity',
        'available_quantity',
        'locked_quantity',
        'status',
        'is_enabled',
        'sort_order',
        'remark',
    ];

    protected function casts(): array
    {
        return [
            'product_id' => 'integer',
            'warehouse_id' => 'integer',
            'source_document_id' => 'integer',
            'production_date' => 'date',
            'expiry_date' => 'date',
            'shelf_life_days' => 'integer',
            'realtime_quantity' => 'decimal:4',
            'available_quantity' => 'decimal:4',
            'locked_quantity' => 'decimal:4',
            'is_enabled' => 'boolean',
            'sort_order' => 'integer',
        ];
    }

    public function getDberpAuditActionPrefix(): string
    {
        return 'dberp.inventory_batch';
    }

    /**
     * @return list<string>
     */
    public function getDberpAuditFields(): array
    {
        return $this->getFillable();
    }

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

    public function warehouse(): BelongsTo
    {
        return $this->belongsTo(DberpWarehouse::class, 'warehouse_id');
    }

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

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