<?php

declare(strict_types=1);

namespace App\Filament\Resources\DberpInventoryBalances\Pages;

use App\Filament\Resources\DberpInventoryBalances\DberpInventoryBalanceResource;
use App\Services\DberpAuditService;
use Illuminate\Database\Eloquent\Model;
use App\Filament\Resources\Pages\EditRecord;

class EditDberpInventoryBalance extends EditRecord
{
    protected static string $resource = DberpInventoryBalanceResource::class;

    protected function handleRecordUpdate(Model $record, array $data): Model
    {
        $before = ['safety_stock' => (string) $record->getAttribute('safety_stock')];

        $record->update($data);

        if ($before['safety_stock'] !== (string) $record->getAttribute('safety_stock')) {
            app(DberpAuditService::class)->log(
                'dberp.inventory_warning.rule_updated',
                $record,
                $before,
                ['safety_stock' => (string) $record->getAttribute('safety_stock')],
            );
        }

        return $record;
    }
}