<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

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');
    }
}