<?php

declare(strict_types=1);

namespace apexl\Io\modules\user\hooks;

use apexl\Io\includes\Entity;
use apexl\Io\interfaces\AbstractHook;
use apexl\Io\modules\user\services\CurrentUserFactory;

final readonly class AddModifiedByToEntitiesOnSave implements AbstractHook
{
    public function __construct(
        private CurrentUserFactory $currentUserFactory,
    ) {}

    public function hooks(): array
    {
        return [Entity::HOOK__BEFORE_STORE];
    }

    public function __invoke(array $data, Entity $entity): array
    {
        $currentUser = $this->currentUserFactory->get();
        if ($entity->isDataField('modified_by')) {
            $data['modified_by'] = $currentUser?->id ?? 0;
        }

        return $data;
    }
}
