<?php

declare(strict_types=1);

namespace PpPhp\Core;

class ProjectNav
{
    /**
     * Modules that need a project database — locked on a standalone project.
     * Events (table-record hooks) and Notifications (per-record alerts compiled
     * into table events) are as DB-bound as Tables/Query/Pages/Fields/Security,
     * so they're locked too — otherwise they open blank on a standalone app.
     * Workspace + Misc stay open: both already render a standalone-aware view.
     */
    public const DB_ONLY = ['/tables', '/query', '/pages', '/fields', '/security', '/events', '/notifications'];

    /**
     * Return the standard project SPA navigation items for the sidebar.
     *
     * @param string $activeUri   The current URI to mark active (e.g. '/tables').
     * @param bool   $standalone  When true, DB-only modules are flagged `locked`.
     */
    public static function items(string $activeUri = '', bool $standalone = false): array
    {
        $all = [
            ['uri' => '/workspace', 'label' => 'Workspace', 'icon' => 'grid-1x2',          'group' => 'Overview',        'key' => 'W'],
            ['uri' => '/tables',    'label' => 'Tables',    'icon' => 'table',             'group' => 'Data',            'key' => 'T'],
            ['uri' => '/query',     'label' => 'Query',     'icon' => 'terminal',          'group' => 'Data',            'key' => 'Q'],
            ['uri' => '/pages',     'label' => 'Pages',     'icon' => 'file-earmark',      'group' => 'Data',            'key' => 'P'],
            ['uri' => '/fields',    'label' => 'Fields',    'icon' => 'input-cursor-text', 'group' => 'Data',            'key' => 'F'],
            ['uri' => '/designer',  'label' => 'Designer',  'icon' => 'palette',           'group' => 'Design',          'key' => 'D'],
            ['uri' => '/style',     'label' => 'Style',     'icon' => 'brush',             'group' => 'Design',          'key' => 'Y'],
            ['uri' => '/mobile',    'label' => 'Mobile',    'icon' => 'phone',             'group' => 'Design',          'key' => 'B'],
            ['uri' => '/events',    'label' => 'Events',    'icon' => 'lightning-charge',  'group' => 'Logic & security', 'key' => 'E'],
            ['uri' => '/notifications', 'label' => 'Notifications', 'icon' => 'bell',       'group' => 'Logic & security', 'key' => 'N'],
            ['uri' => '/security',  'label' => 'Security',  'icon' => 'shield-lock',       'group' => 'Logic & security', 'key' => 'S'],
            ['uri' => '/misc',      'label' => 'Misc',      'icon' => 'three-dots',        'group' => 'Configure',       'key' => 'M'],
            ['uri' => '/custom-files', 'label' => 'Custom Files', 'icon' => 'folder2-open','group' => 'Configure',       'key' => 'C'],
            ['uri' => '/output',    'label' => 'Output',    'icon' => 'box-arrow-right',   'group' => 'Ship',            'key' => 'O'],
            ['uri' => '/finished',  'label' => 'Finished',  'icon' => 'check-circle',      'group' => 'Ship',            'key' => 'I'],
            ['uri' => '/deploy',    'label' => 'Deploy',    'icon' => 'cloud-arrow-up',    'group' => 'Ship',            'key' => 'L'],
        ];

        foreach ($all as &$item) {
            $item['active'] = ($item['uri'] === $activeUri);
            $item['locked'] = $standalone && in_array($item['uri'], self::DB_ONLY, true);
        }
        return $all;
    }

    /**
     * Return all SPA pages (including Workspace) for the toolbar dropdown.
     * Used by the central project navbar. On a standalone project the DB-only
     * pages carry `locked => true` so the dropdown can disable them, matching
     * the sidebar (they'd otherwise navigate to a blank screen).
     */
    public static function spaPages(bool $standalone = false): array
    {
        return array_map(static fn(array $item): array => [
            'uri' => $item['uri'],
            'label' => $item['label'],
            'icon' => $item['icon'],
            'group' => $item['group'],
            'key' => $item['key'],
            'locked' => $standalone && in_array($item['uri'], self::DB_ONLY, true),
        ], self::items());
    }
}
