// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
  compatibilityDate: '2025-07-15',
  devtools: { enabled: true },
  modules: [
    '@nuxtjs/i18n',
    '@nuxtjs/tailwindcss',
    '@pinia/nuxt',
    '@nuxtjs/leaflet',
    'nuxt-oidc-auth',
    '@sentry/nuxt/module',
    '@nuxt/eslint',
    'nuxt-mapbox',
    '@nuxt/icon',
    '@formkit/nuxt'
  ],
  css: ['~/assets/css/tailwind.css'],
  oidc: {
    defaultProvider: 'logto',
    middleware: {
      globalMiddlewareEnabled: false
    },
    session: {
      automaticRefresh: true,
      expirationCheck: true,
      maxAge: 60 * 60 * 24,
      cookie: {
        sameSite: 'lax',
        secure: process.env.NODE_ENV === 'production'
      }
    },
    providers: {
      logto: {
        baseUrl: process.env.NUXT_OIDC_PROVIDERS_LOGTO_BASE_URL || 'https://auth.optovia.ru',
        clientId: process.env.NUXT_OIDC_PROVIDERS_LOGTO_CLIENT_ID || '',
        clientSecret: process.env.NUXT_OIDC_PROVIDERS_LOGTO_CLIENT_SECRET || '',
        redirectUri: process.env.NUXT_OIDC_PROVIDERS_LOGTO_REDIRECT_URI
          || `${process.env.NUXT_PUBLIC_SITE_URL || 'http://localhost:3000'}/auth/logto/callback`,
        scope: [
          'openid',
          'profile',
          'email',
          'offline_access',
          'urn:logto:scope:organizations',
          'urn:logto:scope:organization_token',
          // Teams API scopes
          'read:teams',
          'invite:members',
          // Exchange API scopes
          'read:requests',
          'create:requests',
          'create:offers',
          // Orders API scopes
          'read:orders'
        ],
        additionalAuthParameters: {
          resource: [
            'https://teams.optovia.ru',
            'https://orders.optovia.ru',
            'https://kyc.optovia.ru',
            'https://exchange.optovia.ru'
          ]
        },
        callbackRedirectUrl: process.env.NUXT_OIDC_CALLBACK_REDIRECT_URL || '/dashboard',
        logoutRedirectUri: process.env.NUXT_OIDC_LOGOUT_REDIRECT_URI || process.env.NUXT_PUBLIC_SITE_URL || 'https://optovia.ru/',
        exposeAccessToken: true,
        exposeIdToken: true
      }
    }
  },
  i18n: {
    strategy: 'prefix_and_default',
    defaultLocale: 'ru',
    lazy: true,
    langDir: 'locales',
    locales: [
      { code: 'ru', file: 'ru.json', name: 'Русский' },
      { code: 'en', file: 'en.json', name: 'English' }
    ]
  },
  imports: {
    dirs: ['stores/**']
  },
  components: [
    { path: '~/components', pathPrefix: false },
    { path: '~/components/ui', pathPrefix: false }
  ],
  ssr: true,
  app: {
    baseURL: '/',
    layoutTransition: { name: 'layout', mode: 'out-in' }
  },
  nitro: {
    preset: 'node-server',
    storage: {
      oidc: {
        driver: 'fs',
        base: process.env.OIDC_SESSION_PATH || './.data/sessions'
      }
    }
  },
  devServer: {
    host: '0.0.0.0',
    port: 3000
  },
  vite: {
    server: {
      fs: {
        strict: false
      }
    }
  },
  runtimeConfig: {
    public: {
      odooApiUrl: process.env.NUXT_PUBLIC_ODOO_API_URL,
      teamsGraphqlUser: process.env.NUXT_PUBLIC_TEAMS_GRAPHQL_USER,
      teamsGraphqlTeam: process.env.NUXT_PUBLIC_TEAMS_GRAPHQL_TEAM,
      exchangeGraphqlPublic: process.env.NUXT_PUBLIC_EXCHANGE_GRAPHQL_PUBLIC,
      exchangeGraphqlTeam: process.env.NUXT_PUBLIC_EXCHANGE_GRAPHQL_TEAM,
      kycGraphqlTeam: process.env.NUXT_PUBLIC_KYC_GRAPHQL_TEAM,
      ordersGraphqlTeam: process.env.NUXT_PUBLIC_ORDERS_GRAPHQL_TEAM,
      terminusGraphql: process.env.NUXT_PUBLIC_TERMINUS_GRAPHQL,
      siteUrl: process.env.NUXT_PUBLIC_SITE_URL || 'http://localhost:3000',
      novuAppId: process.env.NUXT_PUBLIC_NOVU_APP_ID,
      novuBackendUrl: process.env.NUXT_PUBLIC_NOVU_BACKEND_URL,
      novuSocketUrl: process.env.NUXT_PUBLIC_NOVU_SOCKET_URL,
      sentryDsn: process.env.NUXT_PUBLIC_SENTRY_DSN
    }
    // NOVU_SECRET_KEY available via server runtimeConfig by default
  },
  sentry: {
    dsn: process.env.NUXT_PUBLIC_SENTRY_DSN,
    environment: process.env.NODE_ENV || 'production',
    tracesSampleRate: 0.1
  },
  mapbox: {
    accessToken: process.env.NUXT_PUBLIC_MAPBOX_ACCESS_TOKEN || ''
  }
})
