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: 'Автотестирование (issues/27-test)', link: '/infrastructure/autotesting' },
          { text: 'Backend: внешние сервисы (test/stage/prod)', link: '/infrastructure/backend-external-services' },
          {
            text: 'Локальная ферма k8s (Multipass)',
            collapsed: false,
            items: [
              { text: 'Обзор', link: '/infrastructure/local-k8s/' },
              { text: 'Доступы и /etc/hosts', link: '/infrastructure/local-k8s/access' },
              { text: 'Запуск 4 VM', link: '/infrastructure/local-k8s/local-nonprod' },
              { text: 'CI/CD и Loki', link: '/infrastructure/local-k8s/ci-and-logs' },
              { text: 'Чеклист проверки', link: '/infrastructure/local-k8s/checklist-verify' },
              { text: 'Git-flow (кратко)', link: '/infrastructure/local-k8s/git-flow' },
              { text: 'Argo CD: test-contour и promtail-test', link: '/infrastructure/local-k8s/argocd-contours' },
              { text: 'Redmine #27: ветки issues/27', link: '/infrastructure/local-k8s/issues-27-branches' },
              { text: 'Redmine ↔ Gitea (MR)', link: '/infrastructure/local-k8s/redmine-gitea' }
            ]
          },
          {
            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: 'Redmine', link: '/infrastructure/test-contour/guides/redmine' },
              { text: 'Что сделано + перенос на сервер', link: '/infrastructure/test-contour/test-contour-article' },
              { text: 'Git-flow: ветки и релизы', link: '/infrastructure/test-contour/git-flow' },
              { text: 'Система тегов CI/CD', link: '/infrastructure/test-contour/tags' },
              { text: 'ArgoCD: sova-root и data-test', link: '/infrastructure/test-contour/argocd-apps' },
              {
                text: 'Справочник k3s-test (все файлы)',
                collapsed: true,
                items: [
                  { text: 'Обзор', link: '/infrastructure/test-contour/k3s-test-reference/' },
                  { text: 'scripts/', link: '/infrastructure/test-contour/k3s-test-reference/scripts' },
                  { text: 'sova-deploy/', link: '/infrastructure/test-contour/k3s-test-reference/sova-deploy' },
                  { text: 'sova-platform/', link: '/infrastructure/test-contour/k3s-test-reference/sova-platform' },
                  { text: 'sova-mocks/', link: '/infrastructure/test-contour/k3s-test-reference/sova-mocks' },
                  { text: 'sova-redmine/', link: '/infrastructure/test-contour/k3s-test-reference/sova-redmine' },
                  { text: 'sova-backend/', link: '/infrastructure/test-contour/k3s-test-reference/sova-backend' },
                  { text: 'sova-adminpanel/', link: '/infrastructure/test-contour/k3s-test-reference/sova-adminpanel' },
                  { text: 'sova-cabinet/', link: '/infrastructure/test-contour/k3s-test-reference/sova-cabinet' },
                  { text: 'sova-docs/', link: '/infrastructure/test-contour/k3s-test-reference/sova-docs-repo' },
                  { text: 'Корень и артеfacts', link: '/infrastructure/test-contour/k3s-test-reference/root-and-artifacts' }
                ]
              }
            ]
          },
          { text: 'Эксплуатация', link: '/operations/maintenance' }
        ]
      }
    ],
    socialLinks: []
  }
})
