<?php

declare(strict_types=1);

namespace Kylin\Agriculture\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Kylin\Agriculture\Enums\MaterialLogType;

/**
 * 物料日志模型
 *
 * 记录物料出入库流水。
 *
 * @property string $type      出入库类型
 * @property float  $quantity  数量
 * @property string $remark    备注
 */
class MaterialLog extends Model
{
    protected $table = 'kylin_agri_material_logs';

    protected $fillable = [
        'material_id',
        'type',
        'quantity',
        'operator_id',
        'remark',
    ];

    protected function casts(): array
    {
        return [
            'type' => MaterialLogType::class,
            'quantity' => 'decimal:2',
        ];
    }

    public function material(): BelongsTo
    {
        return $this->belongsTo(Material::class);
    }

    public function operator(): BelongsTo
    {
        return $this->belongsTo(config('auth.providers.users.model'), 'operator_id');
    }
}
