// https://nuxt.com/docs/api/configuration/nuxt-config
import vuetify, { transformAssetUrls } from 'vite-plugin-vuetify'
import { $fetch } from 'ofetch' 

export default defineNuxtConfig({
  compatibilityDate: '2025-07-15',
  components: true,
  devtools: { enabled: true },
  modules: [
    '@nuxt/eslint',
    '@bootstrap-vue-next/nuxt',
    '@pinia/nuxt',
    'pinia-plugin-persistedstate/nuxt',
    '@nuxtjs/sitemap',
    (_options, nuxt) => {
      nuxt.hooks.hook('vite:extendConfig', (config) => {
        // @ts-expect-error
        config.plugins.push(vuetify({ autoImport: true }))
      })
    }
  ],
  css: [
    '~/assets/scss/main.scss',
    'bootstrap/dist/css/bootstrap.min.css',
  ],
  vite: {
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@use "~/assets/scss/core/variables.scss" as *; @use "~/assets/scss/core/mixins.scss" as *;'
        }
      }
    },
    vue: {
      template: {
        transformAssetUrls,
      },
    },
  },
  
  sitemap: {
    exclude: ['/admin', '/admin/**', '/editar', '/editar/**'],
    urls: async () => {
      const baseURL = process.env.NUXT_PUBLIC_BASE_URL || 'https://miapi.com'
      const routes: string[] = []

      try {
        const products = await $fetch(`${baseURL}products/`)
        routes.push(...products.map((p: any) => `/productos/${p.id}`))
      } catch (error) {
        console.error('Error fetching products for sitemap:', error)
      }

      try {
        const companies = await $fetch(`${baseURL}companies/`)
        routes.push(...companies.map((c: any) => `/c/${c.id}`))
      } catch (error) {
        console.error('Error fetching companies for sitemap:', error)
      }

      routes.push('/busqueda?order=newest')
      return routes
    }
  },

  runtimeConfig: {
    baseURL: process.env.BASE_URL,
    public: {
      baseURL: process.env.NUXT_PUBLIC_BASE_URL,
      googleAnalyticsId: process.env.NUXT_PUBLIC_GOOGLE_ANALYTICS_ID,
      googleMapsApiKey: process.env.NUXT_PUBLIC_GOOGLE_MAPS_API_KEY,
      googleClientId: process.env.NUXT_PUBLIC_GOOGLE_CLIENT_ID,
      facebookId: process.env.NUXT_PUBLIC_FACEBOOK_ID,
    },
    private: {
      apiBase: process.env.NUXT_PUBLIC_BASE_URL,
    },
  },

  build: {
    transpile: ['vuetify'],
  },
})