<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class DberpInventoryException extends Model
{
    protected $table = 'dberp_inventory_exceptions';

    /**
     * @var list<string>
     */
    protected $fillable = [
        'exception_key',
        'exception_type',
        'severity',
        'title',
        'description',
        'product_id',
        'warehouse_id',
        'batch_id',
        'serial_number_id',
        'stocktake_difference_id',
        'replenishment_suggestion_id',
        'status',
        'occurred_at',
        'resolved_at',
        'handled_by',
        'handling_remark',
        'context_snapshot',
        'sort_order',
    ];

    protected function casts(): array
    {
        return [
            'product_id' => 'integer',
            'warehouse_id' => 'integer',
            'batch_id' => 'integer',
            'serial_number_id' => 'integer',
            'stocktake_difference_id' => 'integer',
            'replenishment_suggestion_id' => 'integer',
            'occurred_at' => 'datetime',
            'resolved_at' => 'datetime',
            'handled_by' => 'integer',
            'context_snapshot' => 'array',
            'sort_order' => 'integer',
        ];
    }

    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 serialNumber(): BelongsTo
    {
        return $this->belongsTo(DberpInventorySerialNumber::class, 'serial_number_id');
    }

    public function stocktakeDifference(): BelongsTo
    {
        return $this->belongsTo(DberpStocktakeDifference::class, 'stocktake_difference_id');
    }

    public function replenishmentSuggestion(): BelongsTo
    {
        return $this->belongsTo(DberpInventoryReplenishmentSuggestion::class, 'replenishment_suggestion_id');
    }

    public function handler(): BelongsTo
    {
        return $this->belongsTo(User::class, 'handled_by');
    }
}