<?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 DberpInventorySerialNumber extends Model
{
    use HasDberpAuditTrail;

    protected $table = 'dberp_inventory_serial_numbers';

    /**
     * @var list<string>
     */
    protected $fillable = [
        'product_id',
        'warehouse_id',
        'batch_id',
        'serial_no',
        'source_system',
        'source_document_type',
        'source_document_id',
        'source_document_no',
        'external_reference_id',
        'current_owner_type',
        'current_owner_id',
        'inbound_at',
        'outbound_at',
        'status',
        'is_enabled',
        'sort_order',
        'remark',
    ];

    protected function casts(): array
    {
        return [
            'product_id' => 'integer',
            'warehouse_id' => 'integer',
            'batch_id' => 'integer',
            'source_document_id' => 'integer',
            'current_owner_id' => 'integer',
            'inbound_at' => 'datetime',
            'outbound_at' => 'datetime',
            'is_enabled' => 'boolean',
            'sort_order' => 'integer',
        ];
    }

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

    /**
     * @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 batch(): BelongsTo
    {
        return $this->belongsTo(DberpInventoryBatch::class, 'batch_id');
    }

    public function logs(): HasMany
    {
        return $this->hasMany(DberpInventorySerialNumberLog::class, 'serial_number_id');
    }

    public function inventoryExceptions(): HasMany
    {
        return $this->hasMany(DberpInventoryException::class, 'serial_number_id');
    }

    public function stocktakeSerialNumberDifferences(): HasMany
    {
        return $this->hasMany(DberpStocktakeSerialNumberDifference::class, 'serial_no', 'serial_no');
    }
}
