<?php
namespace Components\Data\Live;

use Components\Base;
use Components\Data\Contracts\NavigationProvider;

/**
 * Live provider; replace groups() with real data source (DB/API) when available.
 */
class LiveNavigationProvider implements NavigationProvider
{
    public function groups(): array
    {
        // For now mirror the mock so UI is consistent; swap to real data later.
        return [
            [
                'header' => mh_lng('MAILBEEZ_APP_HEADER_GENERAL', 'General'),
                'items' => [
                    [
                        'key' => 'dashboard',
                        'label' => mh_lng('MAILBEEZ_APP_LABEL_DASHBOARD', 'Dashboard'),
                        'page' => 'pages/dashboard.php',
                        'submenu' => null,
                        'icon' => \Components\Base::SVG_CHART_DONUT
                    ],
                    /*
                    [
                        'key' => 'reports',
                        'label' => mh_lng('MAILBEEZ_APP_LABEL_REPORTS', 'Reports'),
                        'page' => 'pages/reports.php',
                        'submenu' => null,
                        'icon' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
  <path fill-rule="evenodd" d="M2.25 13.5a8.25 8.25 0 0 1 8.25-8.25.75.75 0 0 1 .75.75v6.75H18a.75.75 0 0 1 .75.75 8.25 8.25 0 0 1-16.5 0Z" clip-rule="evenodd" />
  <path fill-rule="evenodd" d="M12.75 3a.75.75 0 0 1 .75-.75 8.25 8.25 0 0 1 8.25 8.25.75.75 0 0 1-.75.75h-7.5a.75.75 0 0 1-.75-.75V3Z" clip-rule="evenodd" />
</svg>
',
                        'items' => [
                            [
                                'key' => 'report_sales',
                                'label' => mh_lng('MAILBEEZ_APP_LABEL_REPORT_SALES', 'Sales Report'),
                                'page' => 'pages/reports/sales.php',
                            ],
                            [
                                'key' => 'report_customers',
                                'label' => mh_lng('MAILBEEZ_APP_LABEL_REPORT_CUSTOMERS', 'Customer Report'),
                                'page' => 'pages/reports/customers.php',
                            ],
                        ]
                    ],
                    */
/*
                    [
                        'key' => 'wizard',
                        'label' => 'Setup Wizard',
                        'page' => 'pages/wizard.php',
                        'submenu' => null,
                        'icon' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
  <path fill-rule="evenodd" d="M9 4.5a.75.75 0 0 1 .721.544l.813 2.846a3.75 3.75 0 0 0 2.576 2.576l2.846.813a.75.75 0 0 1 0 1.442l-2.846.813a3.75 3.75 0 0 0-2.576 2.576l-.813 2.846a.75.75 0 0 1-1.442 0l-.813-2.846a3.75 3.75 0 0 0-2.576-2.576l-2.846-.813a.75.75 0 0 1 0-1.442l2.846-.813A3.75 3.75 0 0 0 7.466 7.89l.813-2.846A.75.75 0 0 1 9 4.5ZM18 1.5a.75.75 0 0 1 .728.568l.258 1.036c.236.94.97 1.674 1.91 1.91l1.036.258a.75.75 0 0 1 0 1.456l-1.036.258c-.94.236-1.674.97-1.91 1.91l-.258 1.036a.75.75 0 0 1-1.456 0l-.258-1.036a2.625 2.625 0 0 0-1.91-1.91l-1.036-.258a.75.75 0 0 1 0-1.456l1.036-.258a2.625 2.625 0 0 0 1.91-1.91l.258-1.036A.75.75 0 0 1 18 1.5ZM16.5 15a.75.75 0 0 1 .712.513l.394 1.183c.15.447.5.799.948.948l1.183.395a.75.75 0 0 1 0 1.422l-1.183.395c-.447.15-.799.5-.948.948l-.395 1.183a.75.75 0 0 1-1.422 0l-.395-1.183a1.5 1.5 0 0 0-.948-.948l-1.183-.395a.75.75 0 0 1 0-1.422l1.183-.395c.447-.15.799-.5.948-.948l.395-1.183A.75.75 0 0 1 16.5 15Z" clip-rule="evenodd" />
</svg>
'
                    ],
*/
                ],
            ],
            [
                'header' => mh_lng('MAILBEEZ_APP_HEADER_CAMPAIGNS', 'Campaigns'),
                'items' => [
                    [
                        'key' => 'trigger',
                        'label' => mh_lng('MAILBEEZ_APP_LABEL_AUTOMATIONS', 'Automations'),
                        'page' => 'pages/trigger.php',
                        'submenu' => null,
                        'icon' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="-rotate-15">
  <path d="M3.478 2.404a.75.75 0 0 0-.926.941l2.432 7.905H13.5a.75.75 0 0 1 0 1.5H4.984l-2.432 7.905a.75.75 0 0 0 .926.94 60.519 60.519 0 0 0 18.445-8.986.75.75 0 0 0 0-1.218A60.517 60.517 0 0 0 3.478 2.404Z" />
</svg>
'
                    ],
                    [
                        'key' => 'newsletter',
                        'label' => mh_lng('MAILBEEZ_APP_LABEL_NEWSLETTER', 'Newsletter'),
                        'page' => 'pages/newsletter.php',
                        'submenu' => null,
                        'icon' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
  <path d="M1.5 8.67v8.58a3 3 0 0 0 3 3h15a3 3 0 0 0 3-3V8.67l-8.928 5.493a3 3 0 0 1-3.144 0L1.5 8.67Z" />
  <path d="M22.5 6.908V6.75a3 3 0 0 0-3-3h-15a3 3 0 0 0-3 3v.158l9.714 5.978a1.5 1.5 0 0 0 1.572 0L22.5 6.908Z" />
</svg>
'
                    ],



                    /*

                    */
                ],
            ],
            [
                'header' => mh_lng('MAILBEEZ_APP_HEADER_TARGETGROUPS', 'Target Groups'),
                'items' => [
                    [
                        'key' => 'audience',
                        'label' => mh_lng('MAILBEEZ_APP_LABEL_AUDIENCE', 'Audience'),
                        'page' => 'pages/audience.php',
                        'submenu' => null,
                        'icon' => Base::SVG_CIRCLES_OVERLAPPING
                    ],
                    /*
                    [
                        'key' => 'topics',
                        'label' => mh_lng('MAILBEEZ_APP_LABEL_TOPICS', 'Topics'),
                        'page' => 'pages/topics.php',
                        'submenu' => null,
                        'icon' => Base::SVG_TAG
                    ],
                    */
                    [
                        'key' => 'doi_flow',
                        'label' => mh_lng('MAILBEEZ_APP_LABEL_DOI_FLOW', 'DOI Flow'),
                        'page' => 'pages/doi_flow.php',
                        'submenu' => null,
                        'icon' => Base::SVG_TOGGLE
                    ],
                ]

            ],
            [
                'header' => mh_lng('MAILBEEZ_APP_HEADER_CONTENT', 'Content'),
                'items' => [
                    [
                        'key' => 'editor_templates',
                        'label' => mh_lng('MAILBEEZ_APP_LABEL_TEMPLATES', 'Main Templates'),
                        'page' => 'pages/editor_templates.php',
                        'submenu' => null,
                        'icon' => \Components\Base::SVG_PALETTE
                    ],
                    /*
                    [
                        'key' => 'editor_templates_module',
                        'label' => mh_lng('MAILBEEZ_APP_LABEL_MODULE_TEMPLATES', 'Module Templates'),
                        'page' => 'pages/editor_templates_module.php',
                        'submenu' => null,
                        'icon' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
  <path d="M16.5 6a3 3 0 0 0-3-3H6a3 3 0 0 0-3 3v7.5a3 3 0 0 0 3 3v-6A4.5 4.5 0 0 1 10.5 6h6Z" />
  <path d="M18 7.5a3 3 0 0 1 3 3V18a3 3 0 0 1-3 3h-7.5a3 3 0 0 1-3-3v-7.5a3 3 0 0 1 3-3H18Z" />
</svg>
'
                    ],
                    [
                        'key' => 'coupons',
                        'label' => mh_lng('MAILBEEZ_APP_LABEL_COUPONS', 'Coupons'),
                        'page' => 'pages/coupons.php',
                        'submenu' => null,
                        'icon' => \Components\Base::SVG_SOLID_VOUCHER
                    ],
                    */
                ]

            ],
            [
                'header' => mh_lng('MAILBEEZ_APP_HEADER_SETTINGS', 'Settings'),
                'items' => [
                    [
                        'key' => 'configuration',
                        'label' => mh_lng('MAILBEEZ_APP_LABEL_CONFIGURATION', 'Configuration'),
                        'page' => 'pages/configuration.php',
                        'submenu' => 'pages/submenu/configuration.php',
                        'icon' => \Components\Base::SVG_SETTINGS
                    ],
                    /*
                    [
                        'key' => 'datasources',
                        'label' => mh_lng('MAILBEEZ_APP_LABEL_DATASOURCES', 'Data Sources'),
                        'page' => 'pages/datasources.php',
                        'submenu' => null,
                        'icon' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
  <path d="M5.507 4.048A3 3 0 0 1 7.785 3h8.43a3 3 0 0 1 2.278 1.048l1.722 2.008A4.533 4.533 0 0 0 19.5 6h-15c-.243 0-.482.02-.715.056l1.722-2.008Z" />
  <path fill-rule="evenodd" d="M1.5 10.5a3 3 0 0 1 3-3h15a3 3 0 1 1 0 6h-15a3 3 0 0 1-3-3Zm15 0a.75.75 0 1 1-1.5 0 .75.75 0 0 1 1.5 0Zm2.25.75a.75.75 0 1 0 0-1.5.75.75 0 0 0 0 1.5ZM4.5 15a3 3 0 1 0 0 6h15a3 3 0 1 0 0-6h-15Zm11.25 3.75a.75.75 0 1 0 0-1.5.75.75 0 0 0 0 1.5ZM19.5 18a.75.75 0 1 1-1.5 0 .75.75 0 0 1 1.5 0Z" clip-rule="evenodd" />
</svg>
'
                    ],
                    */

                    /*

                    [
                        'key' => 'promo_test',
                        'label' => 'promo test',
                        'page' => 'pages/promo_test.php',
                        'submenu' => null,
                        'icon' => '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
  <path d="M5.507 4.048A3 3 0 0 1 7.785 3h8.43a3 3 0 0 1 2.278 1.048l1.722 2.008A4.533 4.533 0 0 0 19.5 6h-15c-.243 0-.482.02-.715.056l1.722-2.008Z" />
  <path fill-rule="evenodd" d="M1.5 10.5a3 3 0 0 1 3-3h15a3 3 0 1 1 0 6h-15a3 3 0 0 1-3-3Zm15 0a.75.75 0 1 1-1.5 0 .75.75 0 0 1 1.5 0Zm2.25.75a.75.75 0 1 0 0-1.5.75.75 0 0 0 0 1.5ZM4.5 15a3 3 0 1 0 0 6h15a3 3 0 1 0 0-6h-15Zm11.25 3.75a.75.75 0 1 0 0-1.5.75.75 0 0 0 0 1.5ZM19.5 18a.75.75 0 1 1-1.5 0 .75.75 0 0 1 1.5 0Z" clip-rule="evenodd" />
</svg>
'
                    ],
                    */
                ],
            ],
        ];
    }

    public function keyFromPage(string $page): string
    {
        $page = trim(strtolower(str_replace('\\', '/', ltrim((string)$page))));
        if ($page === '') return '';
        if ($page[0] === '/') $page = ltrim($page, '/');
        // Normalize input forms
        $inputFull = $page;
        $inputBase = $this->basenameNoExt($page);
        $inputShort = $inputBase !== '' ? ($inputBase . '.php') : '';

        $exactMatches = [];
        $shortMatches = [];
        $baseMatches = [];

        foreach ($this->flattenGroups() as $item) {
            $key = isset($item['key']) ? strtolower((string)$item['key']) : '';
            if ($key === '') continue;
            $itemPage = isset($item['page']) ? (string)$item['page'] : '';
            if ($itemPage === '' && $key !== '') {
                $itemPage = 'pages/' . $key . '.php';
            }
            $itemPage = strtolower(str_replace('\\', '/', ltrim($itemPage, '/')));
            if ($itemPage === '') continue;

            $itemFull = $itemPage;
            $itemBase = $this->basenameNoExt($itemPage);
            $itemShort = $itemBase !== '' ? ($itemBase . '.php') : '';

            if ($inputFull !== '' && $inputFull === $itemFull) {
                $exactMatches[$key] = true;
            }
            if ($inputShort !== '' && $inputShort === $itemShort) {
                $shortMatches[$key] = true;
            }
            if ($inputBase !== '' && $inputBase === $itemBase) {
                $baseMatches[$key] = true;
            }
        }

        if (!empty($exactMatches)) return array_key_first($exactMatches);
        if (!empty($shortMatches)) {
            // If multiple, ambiguous — return '' to be safe
            return count($shortMatches) === 1 ? array_key_first($shortMatches) : '';
        }
        if (!empty($baseMatches)) {
            return count($baseMatches) === 1 ? array_key_first($baseMatches) : '';
        }
        return '';
    }

    public function getTargetId(string $key): string
    {
        $key = trim((string)$key);
        if ($key === '') {
            return '#app-container';
        }
        $norm = strtolower($key);
        // Allow per-item override via 'targetId' or 'target'
        foreach ($this->flattenGroups() as $item) {
            $k = isset($item['key']) ? strtolower((string)$item['key']) : '';
            if ($k === $norm) {
                $override = $item['targetId'] ?? ($item['target'] ?? null);
                if (is_string($override) && $override !== '') {
                    $override = (string)$override;
                    return $override[0] === '#' ? $override : ('#' . ltrim($override, '#'));
                }
                break;
            }
        }
        return '#app-container-' . $norm;
    }

    private function flattenGroups(): array
    {
        $all = [];
        foreach ($this->groups() as $group) {
            foreach (($group['items'] ?? []) as $item) {
                $all[] = $item;
                if (!empty($item['items'])) {
                    foreach ($item['items'] as $subItem) {
                        $all[] = $subItem;
                    }
                }
            }
        }
        return $all;
    }

    private function basenameNoExt(string $path): string
    {
        $path = trim($path);
        if ($path === '') return '';
        $path = str_replace('\\', '/', $path);
        $base = basename($path);
        $baseLower = strtolower($base);
        if (substr($baseLower, -4) === '.php') {
            $baseLower = substr($baseLower, 0, -4);
        }
        return $baseLower;
    }
}
