<?php

declare(strict_types=1);

namespace Aventa\Examples\Greeting;

use XiunoEvolution\Kernel\PluginRegistrar;
use XiunoEvolution\Kernel\RegistrationContext;
use XiunoEvolution\Kernel\RuntimeContext;

final class GreetingRegistrar implements PluginRegistrar
{
    public function register(RegistrationContext $context): void
    {
        $service = new GreetingService();

        $context->onEvent(
            'example.greeting.render.v1',
            'render-greeting',
            static fn (RuntimeContext $runtime, mixed $payload): string =>
                $service->render($runtime, is_array($payload) ? $payload : []),
        );

        foreach (['install', 'enable', 'disable', 'upgrade', 'uninstall'] as $action) {
            $context->onLifecycle(
                $action,
                static fn (RuntimeContext $runtime) =>
                    $runtime->logger->log('info', 'Example greeting lifecycle action.', ['action' => $action]),
            );
        }

        $context->template('greeting', 'templates/greeting.php');
        $context->asset('greeting-style', 'assets/greeting.css');
        $context->configSchema(
            static fn (string $key, mixed $value): bool =>
                $key === 'greeting.prefix' && is_string($value) && trim($value) !== '',
        );
    }
}
