import { defineConfig } from 'vitepress'

const escapeHtml = (value: string) =>
  value
    .replace(/&/g, '&amp;')
    .replace(/</g, '&lt;')
    .replace(/>/g, '&gt;')
    .replace(/"/g, '&quot;')
    .replace(/'/g, '&#39;')

export default defineConfig({
  lang: 'ru-RU',
  title: 'Sova: документация проекта',
  description: 'Онбординг, запуск и эксплуатация инфраструктуры Sova',
  cleanUrls: true,
  ignoreDeadLinks: true,
  markdown: {
    config(md) {
      const defaultFence = md.renderer.rules.fence

      md.renderer.rules.fence = (tokens, idx, options, env, self) => {
        const token = tokens[idx]
        const info = token.info.trim().split(/\s+/)[0]

        if (info === 'mermaid') {
          return `<pre class="mermaid">${escapeHtml(token.content)}</pre>`
        }

        return defaultFence
          ? defaultFence(tokens, idx, options, env, self)
          : self.renderToken(tokens, idx, options)
      }
    }
  },
  themeConfig: {
    nav: [
      { text: 'Старт', link: '/quick-start' },
      { text: 'Архитектура', link: '/architecture' },
      { text: 'Test-контур k3s', link: '/infrastructure/test-contour/' },
      { text: 'Инфраструктура', link: '/infrastructure/docker' }
    ],
    sidebar: [
      {
        text: 'Онбординг',
        items: [
          { text: 'Обзор', link: '/' },
          { text: 'Быстрый старт', link: '/quick-start' },
          { text: 'Проверка локального контура', link: '/testing' },
          { text: 'Окружение', link: '/environment' },
          { text: 'Архитектура', link: '/architecture' },
          { text: 'Потоки данных', link: '/flows' },
          { text: 'API и Swagger', link: '/api-routes' },
          { text: 'Модели данных', link: '/data-model' },
          { text: 'Документация VitePress', link: '/docs-site' }
        ]
      },
      {
        text: 'Приложения',
        items: [
          { text: 'Backend API', link: '/apps/backend' },
          { text: 'Backend: DDD / бизнес-сущности', link: '/apps/backend-ddd' },
          {
            text: 'Backend: бизнес-сценарии (use cases)',
            collapsed: true,
            items: [
              { text: 'Оглавление', link: '/apps/backend-scenarios/' },
              { text: '1.1 Логин и JWT', link: '/apps/backend-scenarios/login-jwt' },
              { text: '1.2 UID / pcode', link: '/apps/backend-scenarios/auth-uid-pcode' },
              { text: '1.3 Смена региона', link: '/apps/backend-scenarios/change-region' },
              { text: '2.1 Карточка врача и локации', link: '/apps/backend-scenarios/specialist-card-locations' },
              { text: '2.2 Расписание и кеш', link: '/apps/backend-scenarios/schedule-cache' },
              { text: '2.3 GetScheduleMessage', link: '/apps/backend-scenarios/schedule-messenger' },
              { text: '2.4 Расписание: полный мануал (Backend + Cabinet)', link: '/apps/doctor-schedule-sync' },
              { text: '3.1 Анонимная запись', link: '/apps/backend-scenarios/anonymous-reserve' },
              { text: '3.2 SMS и Record', link: '/apps/backend-scenarios/sms-record' },
              { text: '3.3 Киоск checkpass', link: '/apps/backend-scenarios/kiosk-checkpass' },
              { text: '4.1 Синхронизация врачей/отзывов', link: '/apps/backend-scenarios/sync-doctors-reviews' },
              { text: '4.2 Calltouch', link: '/apps/backend-scenarios/calltouch-lead' },
              { text: '4.3 XML-фид Яндекса', link: '/apps/backend-scenarios/xml-yandex-feed' }
            ]
          },
          { text: 'Backend: CRUD контента', link: '/apps/backend-content-crud' },
          { text: 'adminPanel: обзор', link: '/apps/admin-panel' },
          { text: 'adminPanel: CRUD контента', link: '/apps/admin-panel-content-crud' },
          { text: 'Backend: архитектура модулей', link: '/apps/backend-architecture' },
          { text: 'Cabinet', link: '/apps/cabinet' },
          { text: 'Cabinet: онлайн-консультация', link: '/apps/online-consultation' },
          { text: 'Cabinet: архитектура модулей', link: '/apps/cabinet-architecture' }
        ]
      },
      {
        text: 'Инфраструктура',
        items: [
          { text: 'Docker Compose', link: '/infrastructure/docker' },
          { text: 'K8s + Terraform + ArgoCD + Gitea', link: '/infrastructure/k8s-cicd-platform-plan' },
          { text: 'Backend: внешние сервисы (test/stage/prod)', link: '/infrastructure/backend-external-services' },
          {
            text: 'Test-контур k3s (песочница)',
            collapsed: false,
            items: [
              { text: 'Обзор', link: '/infrastructure/test-contour/' },
              { text: 'Визуальные гайды', link: '/infrastructure/test-contour/guides/overview' },
              { text: 'Gitea: теги и CI/CD', link: '/infrastructure/test-contour/guides/gitea-ci' },
              { text: 'ArgoCD: приложения', link: '/infrastructure/test-contour/guides/argocd' },
              { text: 'Grafana / Prometheus / Loki', link: '/infrastructure/test-contour/guides/monitoring' },
              { text: 'Что сделано + перенос на сервер', link: '/infrastructure/test-contour/test-contour-article' },
              { text: 'Система тегов CI/CD', link: '/infrastructure/test-contour/tags' },
              { text: 'ArgoCD: sova-root и data-test', link: '/infrastructure/test-contour/argocd-apps' }
            ]
          },
          { text: 'Эксплуатация', link: '/operations/maintenance' }
        ]
      }
    ],
    socialLinks: []
  }
})
