<?php

namespace FSPoster\App\Pages\Settings\Workflow\Services;

final class WorkflowEventMetadataRegistry
{
    private const DEFAULTS = [
        'supports_offset' => true,
        'allowed_offset_signs' => ['after'],
        'default_offset_sign' => 'after',
        'default_offset_type' => 'minute',
        'queue_mode' => 'realtime',
        'base_time_type' => 'event_occurred_at',
    ];

    private const EVENTS = [
        'schedule_failed' => [
            'allowed_offset_signs' => ['after'],
            'default_offset_sign' => 'after',
            'default_offset_type' => 'minute',
            'queue_mode' => 'realtime',
            'base_time_type' => 'event_occurred_at',
        ],
    ];

    public function get(string $eventKey): array
    {
        return array_merge(self::DEFAULTS, self::EVENTS[$eventKey] ?? []);
    }

    public function applyDefaults(string $eventKey, array $data): array
    {
        $meta = $this->get($eventKey);

        $data['offset_sign'] = $data['offset_sign'] ?? $meta['default_offset_sign'];
        $data['offset_value'] = isset($data['offset_value']) ? (int)$data['offset_value'] : 0;
        $data['offset_type'] = $data['offset_type'] ?? $meta['default_offset_type'];

        return $data;
    }
}
