# retoor <retoor@molodetz.nl>

from devplacepy.docs_api import api_doc_pages

SECTION_GENERAL = "General"
SECTION_TOOLS = "Tools"
SECTION_LEGAL = "Legal"
SECTION_COMPONENTS = "Components"
SECTION_STYLES = "Styles"
SECTION_API = "API"
SECTION_DEVRANT = "devRant API"
SECTION_ADMIN = "Administration"
SECTION_DEVII = "Devii internals"
SECTION_BOTS = "Bots internals"
SECTION_SERVICES = "Services"
SECTION_ARCH = "Architecture"
SECTION_TESTING = "Testing"
SECTION_PROD = "Production"
SECTION_CLAUDE = "Claude Code"

AUDIENCE_START = "Start here"
AUDIENCE_BUILD = "Build with the API"
AUDIENCE_CONTRIBUTE = "Contribute and internals"
AUDIENCE_OPERATE = "Operate"

AUDIENCES = [
    (AUDIENCE_START, [SECTION_GENERAL, SECTION_LEGAL, SECTION_TOOLS]),
    (
        AUDIENCE_BUILD,
        [SECTION_API, SECTION_DEVRANT, SECTION_COMPONENTS, SECTION_STYLES],
    ),
    (
        AUDIENCE_CONTRIBUTE,
        [
            SECTION_ARCH,
            SECTION_SERVICES,
            SECTION_DEVII,
            SECTION_BOTS,
            SECTION_TESTING,
            SECTION_CLAUDE,
        ],
    ),
    (AUDIENCE_OPERATE, [SECTION_ADMIN, SECTION_PROD]),
]

DOCS_PAGES = [
    {"slug": "index", "title": "Overview", "kind": "prose", "section": SECTION_GENERAL},
    {
        "slug": "getting-started",
        "title": "Getting started",
        "kind": "prose",
        "section": SECTION_GENERAL,
    },
    {
        "slug": "getting-started-vibing",
        "title": "Get started with vibing",
        "kind": "prose",
        "section": SECTION_GENERAL,
    },
    {
        "slug": "workspace-editor",
        "title": "The workspace editor",
        "kind": "prose",
        "section": SECTION_GENERAL,
    },
    {
        "slug": "feed",
        "title": "The feed",
        "kind": "prose",
        "section": SECTION_GENERAL,
    },
    {
        "slug": "code-farm",
        "title": "Code Farm",
        "kind": "prose",
        "section": SECTION_GENERAL,
    },
    {
        "slug": "quizzes",
        "title": "Quizzes",
        "kind": "prose",
        "section": SECTION_GENERAL,
    },
    {
        "slug": "opinion-wars",
        "title": "Opinion Wars",
        "kind": "prose",
        "section": SECTION_GENERAL,
    },
    {
        "slug": "block-and-mute",
        "title": "Block and mute",
        "kind": "prose",
        "section": SECTION_GENERAL,
    },
    {
        "slug": "emoji-shortcodes",
        "title": "Emoji shortcodes",
        "kind": "prose",
        "section": SECTION_GENERAL,
    },
    {
        "slug": "devii",
        "title": "Devii Assistant",
        "kind": "prose",
        "section": SECTION_GENERAL,
    },
    {
        "slug": "telegram",
        "title": "Devii on Telegram",
        "kind": "prose",
        "section": SECTION_GENERAL,
    },
    {
        "slug": "dashboard",
        "title": "Your dashboard",
        "kind": "live",
        "section": SECTION_GENERAL,
    },
    {
        "slug": "media-gallery",
        "title": "Media gallery",
        "kind": "prose",
        "section": SECTION_GENERAL,
    },
    {
        "slug": "notification-settings",
        "title": "Notification settings",
        "kind": "prose",
        "section": SECTION_GENERAL,
    },
    {
        "slug": "timezones",
        "title": "Timezone-aware dates",
        "kind": "prose",
        "section": SECTION_GENERAL,
    },
    {
        "slug": "presence",
        "title": "Online presence",
        "kind": "prose",
        "section": SECTION_GENERAL,
    },
    {
        "slug": "awards",
        "title": "Profile awards",
        "kind": "prose",
        "section": SECTION_GENERAL,
    },
    {
        "slug": "ai-correction",
        "title": "AI content correction",
        "kind": "prose",
        "section": SECTION_GENERAL,
    },
    {
        "slug": "ai-modifier",
        "title": "AI modifier",
        "kind": "prose",
        "section": SECTION_GENERAL,
    },
    {
        "slug": "terms",
        "title": "Terms of Service",
        "kind": "prose",
        "section": SECTION_LEGAL,
    },
    {
        "slug": "community-guidelines",
        "title": "Community Guidelines",
        "kind": "prose",
        "section": SECTION_LEGAL,
    },
    {
        "slug": "privacy",
        "title": "Privacy Policy",
        "kind": "prose",
        "section": SECTION_LEGAL,
    },
    {
        "slug": "content-moderation",
        "title": "How moderation works",
        "kind": "prose",
        "section": SECTION_LEGAL,
    },
    {
        "slug": "intellectual-property",
        "title": "Notice and takedown",
        "kind": "prose",
        "section": SECTION_LEGAL,
    },
    {
        "slug": "contact",
        "title": "Contact",
        "kind": "prose",
        "section": SECTION_LEGAL,
    },
    {
        "slug": "moderation-operations",
        "title": "Operating the moderation queue",
        "kind": "prose",
        "section": SECTION_LEGAL,
        "admin": True,
    },
    {
        "slug": "tools-seo",
        "title": "SEO Diagnostics",
        "kind": "prose",
        "section": SECTION_TOOLS,
    },
    {
        "slug": "tools-deepsearch",
        "title": "DeepSearch",
        "kind": "prose",
        "section": SECTION_TOOLS,
    },
    {
        "slug": "tools-isslop",
        "title": "AI Usage Analyzer",
        "kind": "prose",
        "section": SECTION_TOOLS,
    },
    {
        "slug": "isslop-checks",
        "title": "AI Usage Analyzer checks",
        "kind": "prose",
        "section": SECTION_TOOLS,
    },
    {
        "slug": "claude",
        "title": "Claude Code setup",
        "kind": "prose",
        "section": SECTION_CLAUDE,
    },
    {
        "slug": "claude-manual",
        "title": "Manual",
        "kind": "prose",
        "section": SECTION_CLAUDE,
    },
    {
        "slug": "claude-agents",
        "title": "Subagents",
        "kind": "prose",
        "section": SECTION_CLAUDE,
    },
    {
        "slug": "claude-commands",
        "title": "Commands",
        "kind": "prose",
        "section": SECTION_CLAUDE,
    },
    {
        "slug": "claude-workflows",
        "title": "Workflows",
        "kind": "prose",
        "section": SECTION_CLAUDE,
    },
    {
        "slug": "components",
        "title": "Components overview",
        "kind": "prose",
        "section": SECTION_COMPONENTS,
    },
    {
        "slug": "component-dp-avatar",
        "title": "dp-avatar",
        "kind": "prose",
        "section": SECTION_COMPONENTS,
    },
    {
        "slug": "component-dp-code",
        "title": "dp-code",
        "kind": "prose",
        "section": SECTION_COMPONENTS,
    },
    {
        "slug": "component-dp-content",
        "title": "dp-content",
        "kind": "prose",
        "section": SECTION_COMPONENTS,
    },
    {
        "slug": "component-dp-title",
        "title": "dp-title",
        "kind": "prose",
        "section": SECTION_COMPONENTS,
    },
    {
        "slug": "component-dp-upload",
        "title": "dp-upload",
        "kind": "prose",
        "section": SECTION_COMPONENTS,
    },
    {
        "slug": "component-dp-toast",
        "title": "dp-toast",
        "kind": "prose",
        "section": SECTION_COMPONENTS,
    },
    {
        "slug": "component-dp-dialog",
        "title": "dp-dialog",
        "kind": "prose",
        "section": SECTION_COMPONENTS,
    },
    {
        "slug": "component-dp-context-menu",
        "title": "dp-context-menu",
        "kind": "prose",
        "section": SECTION_COMPONENTS,
    },
    {
        "slug": "component-dp-lightbox",
        "title": "dp-lightbox",
        "kind": "prose",
        "section": SECTION_COMPONENTS,
    },
    {
        "slug": "component-devii-terminal",
        "title": "devii-terminal",
        "kind": "prose",
        "section": SECTION_COMPONENTS,
    },
    {
        "slug": "component-devii-avatar",
        "title": "devii-avatar",
        "kind": "prose",
        "section": SECTION_COMPONENTS,
    },
    {
        "slug": "component-emoji-picker",
        "title": "emoji-picker",
        "kind": "prose",
        "section": SECTION_COMPONENTS,
    },
    {
        "slug": "styles",
        "title": "Design system overview",
        "kind": "prose",
        "section": SECTION_STYLES,
    },
    {
        "slug": "styles-colors",
        "title": "Colors",
        "kind": "prose",
        "section": SECTION_STYLES,
    },
    {
        "slug": "styles-layout",
        "title": "Layout",
        "kind": "prose",
        "section": SECTION_STYLES,
    },
    {
        "slug": "styles-responsiveness",
        "title": "Responsiveness",
        "kind": "prose",
        "section": SECTION_STYLES,
    },
    {
        "slug": "styles-consistency",
        "title": "Consistency rules",
        "kind": "prose",
        "section": SECTION_STYLES,
    },
    {
        "slug": "authentication",
        "title": "Authentication",
        "kind": "prose",
        "section": SECTION_API,
    },
    {
        "slug": "xmlrpc",
        "title": "XML-RPC API",
        "kind": "prose",
        "section": SECTION_API,
    },
    {
        "slug": "devrant",
        "title": "Overview",
        "kind": "prose",
        "section": SECTION_DEVRANT,
    },
    {
        "slug": "devrant-auth",
        "title": "Authentication & accounts",
        "kind": "prose",
        "section": SECTION_DEVRANT,
    },
    {
        "slug": "devrant-rants",
        "title": "Rants",
        "kind": "prose",
        "section": SECTION_DEVRANT,
    },
    {
        "slug": "devrant-comments",
        "title": "Comments",
        "kind": "prose",
        "section": SECTION_DEVRANT,
    },
    {
        "slug": "devrant-users",
        "title": "Users & avatars",
        "kind": "prose",
        "section": SECTION_DEVRANT,
    },
    {
        "slug": "devrant-notifications",
        "title": "Notifications",
        "kind": "prose",
        "section": SECTION_DEVRANT,
    },
    {
        "slug": "devrant-clients",
        "title": "Client scripts",
        "kind": "prose",
        "section": SECTION_DEVRANT,
    },
    *[
        {**page, "section": (SECTION_ADMIN if page.get("admin") else SECTION_API)}
        for page in api_doc_pages()
    ],
    {
        "slug": "devii-admin",
        "title": "Devii for admins",
        "kind": "prose",
        "admin": True,
        "section": SECTION_ADMIN,
    },
    {
        "slug": "telegram-admin",
        "title": "Telegram bot operations",
        "kind": "prose",
        "admin": True,
        "section": SECTION_ADMIN,
    },
    {
        "slug": "media-moderation",
        "title": "Media moderation",
        "kind": "prose",
        "admin": True,
        "section": SECTION_ADMIN,
    },
    {
        "slug": "soft-delete",
        "title": "Soft delete and data retention",
        "kind": "prose",
        "admin": True,
        "section": SECTION_ADMIN,
    },
    {
        "slug": "backups",
        "title": "Backups",
        "kind": "prose",
        "admin": True,
        "section": SECTION_ADMIN,
    },
    {
        "slug": "gamification",
        "title": "Gamification and badges",
        "kind": "prose",
        "admin": True,
        "section": SECTION_ADMIN,
    },
    {
        "slug": "audit-log",
        "title": "Audit Log",
        "kind": "prose",
        "admin": True,
        "section": SECTION_ADMIN,
    },
    {
        "slug": "devii-internals",
        "title": "Devii internals",
        "kind": "prose",
        "admin": True,
        "section": SECTION_DEVII,
    },
    {
        "slug": "devii-architecture",
        "title": "Architecture and sessions",
        "kind": "prose",
        "admin": True,
        "section": SECTION_DEVII,
    },
    {
        "slug": "devii-tools",
        "title": "Tools and scopes",
        "kind": "prose",
        "admin": True,
        "section": SECTION_DEVII,
    },
    {
        "slug": "devii-data",
        "title": "Data and persistence",
        "kind": "prose",
        "admin": True,
        "section": SECTION_DEVII,
    },
    {
        "slug": "devii-security",
        "title": "Security and limits",
        "kind": "prose",
        "admin": True,
        "section": SECTION_DEVII,
    },
    {
        "slug": "devii-config",
        "title": "Configuration and CLI",
        "kind": "prose",
        "admin": True,
        "section": SECTION_DEVII,
    },
    {
        "slug": "bots-internals",
        "title": "Bots internals",
        "kind": "prose",
        "admin": True,
        "section": SECTION_BOTS,
    },
    {
        "slug": "bots-architecture",
        "title": "Architecture and lifecycle",
        "kind": "prose",
        "admin": True,
        "section": SECTION_BOTS,
    },
    {
        "slug": "bots-personas",
        "title": "Personas and voice",
        "kind": "prose",
        "admin": True,
        "section": SECTION_BOTS,
    },
    {
        "slug": "bots-content",
        "title": "Content generation and quality",
        "kind": "prose",
        "admin": True,
        "section": SECTION_BOTS,
    },
    {
        "slug": "bots-engagement",
        "title": "Engagement and interaction",
        "kind": "prose",
        "admin": True,
        "section": SECTION_BOTS,
    },
    {
        "slug": "bots-realism",
        "title": "Realism and design decisions",
        "kind": "prose",
        "admin": True,
        "section": SECTION_BOTS,
    },
    {
        "slug": "bots-config",
        "title": "Configuration and operations",
        "kind": "prose",
        "admin": True,
        "section": SECTION_BOTS,
    },
    {
        "slug": "services-overview",
        "title": "Services overview",
        "kind": "prose",
        "admin": True,
        "section": SECTION_SERVICES,
    },
    {
        "slug": "services-framework",
        "title": "Service framework",
        "kind": "prose",
        "admin": True,
        "section": SECTION_SERVICES,
    },
    {
        "slug": "services-data",
        "title": "Reading service data",
        "kind": "prose",
        "admin": True,
        "section": SECTION_SERVICES,
    },
    {
        "slug": "services-gateway",
        "title": "GatewayService (AI)",
        "kind": "prose",
        "admin": True,
        "section": SECTION_SERVICES,
    },
    {
        "slug": "services-devii",
        "title": "DeviiService",
        "kind": "prose",
        "admin": True,
        "section": SECTION_SERVICES,
    },
    {
        "slug": "services-news",
        "title": "NewsService",
        "kind": "prose",
        "admin": True,
        "section": SECTION_SERVICES,
    },
    {
        "slug": "services-bots",
        "title": "BotsService",
        "kind": "prose",
        "admin": True,
        "section": SECTION_SERVICES,
    },
    {
        "slug": "services-zip",
        "title": "ZipService",
        "kind": "prose",
        "admin": True,
        "section": SECTION_SERVICES,
    },
    {
        "slug": "services-containers",
        "title": "Container Manager",
        "kind": "prose",
        "admin": True,
        "section": SECTION_SERVICES,
    },
    {
        "slug": "services-dbapi",
        "title": "Database API service",
        "kind": "prose",
        "admin": True,
        "section": SECTION_SERVICES,
    },
    {
        "slug": "services-pubsub",
        "title": "Pub/Sub service",
        "kind": "prose",
        "admin": True,
        "section": SECTION_SERVICES,
    },
    {
        "slug": "architecture",
        "title": "Architecture overview",
        "kind": "prose",
        "admin": True,
        "section": SECTION_ARCH,
    },
    {
        "slug": "architecture-backend",
        "title": "Backend and request pipeline",
        "kind": "prose",
        "admin": True,
        "section": SECTION_ARCH,
    },
    {
        "slug": "architecture-frontend",
        "title": "Frontend and components",
        "kind": "prose",
        "admin": True,
        "section": SECTION_ARCH,
    },
    {
        "slug": "architecture-styling",
        "title": "Styling and templates",
        "kind": "prose",
        "admin": True,
        "section": SECTION_ARCH,
    },
    {
        "slug": "architecture-conventions",
        "title": "Conventions and rules",
        "kind": "prose",
        "admin": True,
        "section": SECTION_ARCH,
    },
    {
        "slug": "architecture-workflow",
        "title": "Development workflow",
        "kind": "prose",
        "admin": True,
        "section": SECTION_ARCH,
    },
    {
        "slug": "architecture-jobs",
        "title": "Async job services",
        "kind": "prose",
        "admin": True,
        "section": SECTION_ARCH,
    },
    {
        "slug": "testing",
        "title": "Testing overview",
        "kind": "prose",
        "admin": True,
        "section": SECTION_TESTING,
    },
    {
        "slug": "testing-framework",
        "title": "Test framework and rules",
        "kind": "prose",
        "admin": True,
        "section": SECTION_TESTING,
    },
    {
        "slug": "testing-locust",
        "title": "Load testing with Locust",
        "kind": "prose",
        "admin": True,
        "section": SECTION_TESTING,
    },
    {
        "slug": "testing-make",
        "title": "Running tests via make",
        "kind": "prose",
        "admin": True,
        "section": SECTION_TESTING,
    },
    {
        "slug": "testing-cicd",
        "title": "Coverage and CI/CD",
        "kind": "prose",
        "admin": True,
        "section": SECTION_TESTING,
    },
    {
        "slug": "production",
        "title": "Production overview",
        "kind": "prose",
        "admin": True,
        "section": SECTION_PROD,
    },
    {
        "slug": "production-deploy",
        "title": "Deploy and update",
        "kind": "prose",
        "admin": True,
        "section": SECTION_PROD,
    },
    {
        "slug": "production-nginx",
        "title": "nginx and networking",
        "kind": "prose",
        "admin": True,
        "section": SECTION_PROD,
    },
    {
        "slug": "production-concurrency",
        "title": "Multi-worker and concurrency",
        "kind": "prose",
        "admin": True,
        "section": SECTION_PROD,
    },
    {
        "slug": "static-caching",
        "title": "Static asset caching and versioning",
        "kind": "prose",
        "admin": True,
        "section": SECTION_PROD,
    },
]

PAGES_BY_SLUG = {page["slug"]: page for page in DOCS_PAGES}


def nav_groups(pages):
    by_section = {}
    for page in pages:
        by_section.setdefault(page.get("section"), []).append(page)
    groups = []
    for audience, sections in AUDIENCES:
        tiers = []
        for section in sections:
            section_pages = by_section.get(section)
            if section_pages:
                tiers.append((section, section_pages))
        if tiers:
            groups.append((audience, tiers))
    return groups
