import node from '@astrojs/node'
import react from '@astrojs/react'
import starlight from '@astrojs/starlight'
import { ExpressiveCodeTheme } from '@astrojs/starlight/expressive-code'
import { defineConfig } from 'astro/config'
import fs from 'node:fs'

import config from './gt.config.json'
import { generateI18nConfig } from './src/i18n/generateI18nConfig'

const jsonDarkString = fs.readFileSync(
  new URL(`src/assets/themes/daytona-code-dark.json`, import.meta.url),
  'utf-8'
)
const jsonLightString = fs.readFileSync(
  new URL(`src/assets/themes/daytona-code-light.json`, import.meta.url),
  'utf-8'
)
const myThemeDark = ExpressiveCodeTheme.fromJSONString(jsonDarkString)
const myThemeLight = ExpressiveCodeTheme.fromJSONString(jsonLightString)

// https://astro.build/config
export default defineConfig({
  site: process.env.PUBLIC_WEB_URL,
  base: '/docs',
  integrations: [
    react(),
    starlight({
      title: 'Daytona',
      favicon: '/favicon.ico',
      social: [
        {
          icon: 'github',
          label: 'GitHub',
          href: 'https://github.com/daytonaio',
        },
      ],
      editLink: {
        baseUrl: 'https://github.com/daytonaio/daytona/blob/main/apps/docs/',
      },
      tableOfContents: { minHeadingLevel: 2, maxHeadingLevel: 4 },
      customCss: ['./src/fonts/font-face.css', './src/styles/style.scss'],
      components: {
        Footer: './src/components/Footer.astro',
        MarkdownContent: './src/components/MarkdownContent.astro',
        Pagination: './src/components/Pagination.astro',
        Header: './src/components/Header.astro',
        PageSidebar: './src/components/PageSidebar.astro',
        PageFrame: './src/components/PageFrame.astro',
        Sidebar: './src/components/Sidebar.astro',
        TwoColumnContent: './src/components/TwoColumnContent.astro',
        TableOfContents: './src/components/TableOfContents.astro',
        MobileMenuToggle: './src/components/MobileMenuToggle.astro',
        ContentPanel: './src/components/ContentPanel.astro',
        PageTitle: './src/components/PageTitle.astro',
        Hero: './src/components/Hero.astro',
        ThemeProvider: './src/components/ThemeProvider.astro',
        ThemeSelect: './src/components/ThemeSelect.astro',
        Head: './src/components/Head.astro',
        EditLink: './src/components/EditLink.astro',
        ExploreMore: './src/components/ExploreMore.astro',
      },
      expressiveCode: {
        minSyntaxHighlightingColorContrast: 3.0,
        themes: [myThemeDark, myThemeLight],
      },
      ...generateI18nConfig(config),
    }),
  ],
  security: {
    allowedDomains: [
      { hostname: 'daytona.io' },
      { hostname: 'www.daytona.io' },
      { hostname: 'localhost' },
    ],
  },
  output: 'server',
  adapter: node({
    mode: 'middleware',
  }),
  outDir: '../../dist/apps/docs',
  vite: {
    ssr: {
      noExternal: ['path-to-regexp', '@astrojs/react', 'zod'],
    },
  },
})
