import { existsSync, readdirSync } from 'node:fs'
import { resolve } from 'node:path'

const modulesDir = resolve(__dirname, 'modules')
const moduleDirs = existsSync(modulesDir)
    ? readdirSync(modulesDir, { withFileTypes: true })
        .filter((d) => d.isDirectory())
        .map((d) => d.name)
    : []
const moduleLayers = moduleDirs.map((name) => `./modules/${name}`)
const moduleComposableDirs = moduleDirs
    .map((name) => `modules/${name}/composables`)
    .filter((path) => existsSync(resolve(__dirname, path)))
const moduleStoreDirs = moduleDirs
    .map((name) => `modules/${name}/stores`)
    .filter((path) => existsSync(resolve(__dirname, path)))

export default defineNuxtConfig({
    compatibilityDate: '2025-07-15',
    devtools: { enabled: false },
    ssr: false,
    srcDir: '.',
    css: ['~/assets/css/app.css', '~/assets/css/dark.css'],
    app: {
        baseURL: process.env.NODE_ENV === 'production'
            ? (process.env.NUXT_PUBLIC_APP_BASE || '/')
            : '/',
    },
    extends: ['@malio/layer-ui', ...moduleLayers],
    modules: [
        '@nuxtjs/tailwindcss',
        '@pinia/nuxt',
        'nuxt-toast',
        '@nuxtjs/i18n',
        '@nuxt/icon',
        // Error tracking GlitchTip : charge le module uniquement si un DSN est fourni
        // (donc seulement au build prod). En dev, aucun DSN => zero impact.
        ...(process.env.NUXT_PUBLIC_SENTRY_DSN ? ['@sentry/nuxt/module'] : []),
    ],
    dir: {
        layouts: 'app/layouts',
        middleware: 'app/middleware',
    },
    imports: {
        dirs: [
            'shared/composables',
            'shared/stores',
            'shared/utils',
            'composables',
            'stores',
            'utils',
            ...moduleComposableDirs,
            ...moduleStoreDirs,
        ],
    },
    pinia: {
        storesDirs: ['shared/stores/**', 'stores/**', 'modules/*/stores/**'],
    },
    runtimeConfig: {
        public: {
            apiBase: process.env.NUXT_PUBLIC_API_BASE,
            sentry: {
                // DSN du projet GlitchTip "lesstime-front" (vide => SDK inerte).
                dsn: process.env.NUXT_PUBLIC_SENTRY_DSN || '',
                environment: process.env.NODE_ENV || 'development',
            },
        },
    },

    // Upload des source maps vers GlitchTip (stacktraces lisibles cote front).
    // Ne s'execute au build que si un token d'upload est fourni.
    sourcemap: { client: 'hidden' },
    sentry: {
        sourceMapsUploadOptions: {
            url: process.env.SENTRY_URL,
            org: process.env.SENTRY_ORG,
            project: process.env.SENTRY_PROJECT,
            authToken: process.env.SENTRY_AUTH_TOKEN,
        },
    },
    devServer: {
        port: 3002,
    },
    components: [
        { path: '~/components', pathPrefix: false },
    ],
    vite: {
        server: {
            allowedHosts: true,
            proxy: {
                '/api': {
                    target: 'http://nginx',
                    changeOrigin: true,
                },
            },
        },
        optimizeDeps: {
            include: ['markdown-it-task-lists'],
        },
    },
    toast: {
        settings: {
            timeout: 2000,
            closeOnClick: true,
            progressBar: false
        }
    },
    i18n: {
        strategy: 'no_prefix',
        defaultLocale: 'fr',
        langDir: 'locales',
        locales: [
            {code: 'fr', file: 'fr.json', name: 'Français'}
        ],
    },
    typescript: { strict: true },
    build: { transpile: ['@vuepic/vue-datepicker'] },
})
