<?php

namespace App\Modules\Navigation\Services;

/**
 * Groups HR mega-menu browse items into labeled section cards.
 */
final class HrMegaBrowseComposer
{
    /** @var list<string> */
    private const SECTION_ORDER = [
        'self_service',
        'organization',
        'employees',
        'attendance',
        'leave',
        'recruitment',
        'reports',
    ];

    /** @var array<string, string> */
    private const SECTION_ICONS = [
        'self_service' => 'fe fe-user',
        'organization' => 'fe fe-briefcase',
        'employees' => 'fe fe-users',
        'attendance' => 'fe fe-clock',
        'leave' => 'fe fe-calendar',
        'recruitment' => 'fe fe-target',
        'reports' => 'fe fe-bar-chart-2',
    ];

    /**
     * @param  list<array<string, mixed>>  $items
     * @return list<array{key: string, label: string, icon: string, items: list<array<string, mixed>>}>
     */
    public function compose(array $items): array
    {
        $buckets = [];
        foreach ($items as $item) {
            $section = (string) ($item['section'] ?? 'organization');
            $buckets[$section][] = $item;
        }

        $out = [];
        foreach (self::SECTION_ORDER as $key) {
            if (($buckets[$key] ?? []) === []) {
                continue;
            }
            $out[] = $this->section($key, $buckets[$key]);
            unset($buckets[$key]);
        }

        foreach ($buckets as $key => $sectionItems) {
            $out[] = $this->section((string) $key, $sectionItems);
        }

        return $out;
    }

    /**
     * @param  list<array<string, mixed>>  $items
     * @return array{key: string, label: string, icon: string, items: list<array<string, mixed>>}
     */
    private function section(string $key, array $items): array
    {
        return [
            'key' => $key,
            'label' => (string) __('nav.mega.'.$key),
            'icon' => self::SECTION_ICONS[$key] ?? 'fe fe-grid',
            'items' => $items,
        ];
    }
}
