<?php

namespace App\Models;

use App\Models\Scopes\ParentBranchScope;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class CollateralMovement extends Model
{
    protected $fillable = [
        'collateral_id', 'action', 'from_status', 'to_status', 'from_store_room',
        'from_shelf', 'to_store_room', 'to_shelf', 'notes', 'moved_by', 'moved_at',
    ];

    protected static function booted(): void
    {
        static::addGlobalScope(new ParentBranchScope('collaterals', 'collateral_id', 'id', ['movement']));
    }

    protected function casts(): array
    {
        return ['moved_at' => 'datetime'];
    }

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

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