<?php

declare(strict_types=1);

namespace App\Models;

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

class DberpStocktakeDifference extends Model
{
    use HasDberpAuditTrail;

    protected $table = 'dberp_stocktake_differences';

    /**
     * @var list<string>
     */
    protected $fillable = [
        'stocktake_order_id',
        'stocktake_order_item_id',
        'inventory_transaction_id',
        'product_id',
        'product_sku_id',
        'product_sku_code',
        'product_sku_name',
        'product_sku_spec',
        'warehouse_id',
        'book_quantity',
        'actual_quantity',
        'difference_quantity',
        'result_type',
        'status',
        'reason_code',
        'reason_detail',
        'resolution_type',
        'resolution_remark',
        'handled_by',
        'handled_at',
        'sort_order',
    ];

    protected function casts(): array
    {
        return [
            'stocktake_order_id' => 'integer',
            'stocktake_order_item_id' => 'integer',
            'inventory_transaction_id' => 'integer',
            'product_id' => 'integer',
            'product_sku_id' => 'integer',
            'warehouse_id' => 'integer',
            'book_quantity' => 'decimal:4',
            'actual_quantity' => 'decimal:4',
            'difference_quantity' => 'decimal:4',
            'handled_by' => 'integer',
            'handled_at' => 'datetime',
            'sort_order' => 'integer',
        ];
    }

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

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

    public function stocktakeOrder(): BelongsTo
    {
        return $this->belongsTo(DberpStocktakeOrder::class, 'stocktake_order_id');
    }

    public function stocktakeOrderItem(): BelongsTo
    {
        return $this->belongsTo(DberpStocktakeOrderItem::class, 'stocktake_order_item_id');
    }

    public function inventoryTransaction(): BelongsTo
    {
        return $this->belongsTo(DberpInventoryTransaction::class, 'inventory_transaction_id');
    }

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

    public function productSku(): BelongsTo
    {
        return $this->belongsTo(DberpProductSku::class, 'product_sku_id');
    }

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

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