import type { NextConfig } from 'next'
import createNextIntlPlugin from 'next-intl/plugin'
import { buildSecurityHeaders } from './src/libs/security/headers'

const withNextIntl = createNextIntlPlugin()

const isProd = process.env.NODE_ENV === 'production'
const securityHeaders = buildSecurityHeaders({ isProd })

const nextConfig: NextConfig = {
  output: 'standalone',
  poweredByHeader: false,
  reactStrictMode: true,
  images: {
    unoptimized: true,
  },
  experimental: {
    authInterrupts: true,
  },
  async headers() {
    return [
      {
        source: '/:path*',
        headers: securityHeaders,
      },
    ]
  },
  async rewrites() {
    const backendUrl =
      process.env.NEXT_PUBLIC_BACK_END_DOMAIN || 'http://localhost:8080/api/v1'
    return [
      { source: '/api/v1', destination: backendUrl, basePath: false },
      {
        source: '/api/v1/:path*',
        destination: `${backendUrl}/:path*`,
        basePath: false,
      },
    ]
  },
  webpack(config) {
    config.module.rules.push({
      test: /\.yaml$/,
      type: 'asset/source',
    })
    return config
  },
  turbopack: {
    rules: {
      '*.svg': {
        loaders: ['@svgr/webpack'],
        as: '*.js',
      },
      '*.yaml': {
        loaders: ['yaml-loader'],
        as: '*.js',
      },
    },
  },
}

export default withNextIntl(nextConfig)
