<?php

namespace App\Support;

/**
 * Sidebar navigation tree for the public /documentation site. The
 * controller iterates this list to render the left nav and to resolve a
 * slug → page-title + Blade partial. Adding a page is two steps: drop a
 * Blade partial under `resources/views/documentation/pages/{slug}.blade.php`
 * and add an entry here.
 */
final class DocumentationNav
{
    /**
     * @return list<array{title: string, items: list<array{slug: string, title: string}>}>
     */
    public static function tree(): array
    {
        return [
            [
                'title' => 'Get started',
                'items' => [
                    ['slug' => 'welcome', 'title' => 'Welcome'],
                    ['slug' => 'installation', 'title' => 'Install Pitchbar (self-host)'],
                    ['slug' => 'quickstart', 'title' => 'Quickstart'],
                    ['slug' => 'concepts', 'title' => 'Core concepts'],
                ],
            ],
            [
                'title' => 'Build your agent',
                'items' => [
                    ['slug' => 'agents', 'title' => 'Agents'],
                    ['slug' => 'site-types', 'title' => 'Site types & vertical presets'],
                    ['slug' => 'tool-calling', 'title' => 'Tools & rich messages'],
                    ['slug' => 'knowledge', 'title' => 'Knowledge sources'],
                    ['slug' => 'auto-index', 'title' => 'Auto-index visited pages'],
                    ['slug' => 'customize', 'title' => 'Persona, theme & prompts'],
                    ['slug' => 'behavior-rules', 'title' => 'Behavior rules & triggers'],
                    ['slug' => 'curated-answers', 'title' => 'Curated answers & CTAs'],
                    ['slug' => 'workflows', 'title' => 'Workflows'],
                    ['slug' => 'playground', 'title' => 'Playground'],
                ],
            ],
            [
                'title' => 'Embed the widget',
                'items' => [
                    ['slug' => 'embed', 'title' => 'Install snippet'],
                    ['slug' => 'allowed-origins', 'title' => 'Allowed origins'],
                    ['slug' => 'widget-features', 'title' => 'Voice, leads & persistence'],
                ],
            ],
            [
                'title' => 'Run your workspace',
                'items' => [
                    ['slug' => 'inbox', 'title' => 'Inbox & human takeover'],
                    ['slug' => 'live-chat', 'title' => 'Live human handoff'],
                    ['slug' => 'analytics', 'title' => 'Analytics & gaps'],
                    ['slug' => 'billing', 'title' => 'Billing & plans'],
                    ['slug' => 'members', 'title' => 'Members & roles'],
                    ['slug' => 'integrations', 'title' => 'Integrations'],
                    ['slug' => 'internationalization', 'title' => 'Languages & i18n'],
                ],
            ],
            [
                'title' => 'WordPress & WooCommerce',
                'items' => [
                    ['slug' => 'wordpress-integration', 'title' => 'Overview'],
                    ['slug' => 'wordpress-install', 'title' => 'Install & connect'],
                    ['slug' => 'wordpress-sync', 'title' => 'Content sync'],
                    ['slug' => 'wordpress-page-builders', 'title' => 'Page builders'],
                    ['slug' => 'wordpress-woocommerce', 'title' => 'WooCommerce deep links'],
                    ['slug' => 'wordpress-rest-api', 'title' => 'REST API reference'],
                    ['slug' => 'wordpress-troubleshooting', 'title' => 'Troubleshooting'],
                ],
            ],
            [
                'title' => 'Platform admin',
                'items' => [
                    ['slug' => 'admin-overview', 'title' => 'Platform overview'],
                    ['slug' => 'admin-plans', 'title' => 'Plans & Stripe sync'],
                    ['slug' => 'admin-health', 'title' => 'Site health & failed jobs'],
                    ['slug' => 'admin-board', 'title' => 'Internal Kanban board'],
                    ['slug' => 'changelog', 'title' => 'Application changelog'],
                ],
            ],
            [
                'title' => 'API reference',
                'items' => [
                    ['slug' => 'widget-api', 'title' => 'Widget API'],
                    ['slug' => 'webhooks', 'title' => 'Outgoing webhooks'],
                ],
            ],
            [
                'title' => 'Architecture',
                'items' => [
                    ['slug' => 'architecture', 'title' => 'Stack & layout'],
                    ['slug' => 'data-model', 'title' => 'Data model'],
                    ['slug' => 'rag-pipeline', 'title' => 'RAG pipeline'],
                    ['slug' => 'hot-path', 'title' => 'Hot path & latency'],
                    ['slug' => 'multi-tenancy', 'title' => 'Multi-tenancy'],
                    ['slug' => 'security', 'title' => 'Security model'],
                ],
            ],
            [
                'title' => 'Operate',
                'items' => [
                    ['slug' => 'env', 'title' => 'Environment variables'],
                    ['slug' => 'deployment', 'title' => 'Deployment'],
                    ['slug' => 'observability', 'title' => 'Observability'],
                    ['slug' => 'whitelabel-audit', 'title' => 'White-label audit'],
                    ['slug' => 'seo', 'title' => 'SEO surface'],
                    ['slug' => 'cloudflare-costs', 'title' => 'Cloudflare API costs & usage'],
                ],
            ],
        ];
    }

    /**
     * @return array<string, array{title: string, group: string}>
     */
    public static function flat(): array
    {
        $out = [];
        foreach (self::tree() as $group) {
            foreach ($group['items'] as $item) {
                $out[$item['slug']] = [
                    'title' => $item['title'],
                    'group' => $group['title'],
                ];
            }
        }

        return $out;
    }

    /**
     * @return array{prev: ?array{slug: string, title: string}, next: ?array{slug: string, title: string}}
     */
    public static function neighbors(string $slug): array
    {
        $linear = [];
        foreach (self::tree() as $group) {
            foreach ($group['items'] as $item) {
                $linear[] = $item;
            }
        }

        $idx = null;
        foreach ($linear as $i => $item) {
            if ($item['slug'] === $slug) {
                $idx = $i;
                break;
            }
        }

        if ($idx === null) {
            return ['prev' => null, 'next' => null];
        }

        return [
            'prev' => $idx > 0 ? $linear[$idx - 1] : null,
            'next' => $idx < count($linear) - 1 ? $linear[$idx + 1] : null,
        ];
    }
}
