import { fileURLToPath, URL } from 'url'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import postCssPxToRem from 'postcss-pxtorem'
import { resolve } from 'path'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'


export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), '');
  return {
    plugins: [
      vue(),
      vueJsx(),
      createSvgIconsPlugin({
        iconDirs: [resolve(process.cwd(), 'src/assets/icons')],
        symbolId: 'icon-[dir]-[name]',
      }),
    ],
    productionSourceMap: false,
    configureWebpack: {
      devtool: false
    },
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      }
    },
    server: {
      port: 3002,
      host: '0.0.0.0',
      https: false,
      proxy: {
        // 系统管理服务19901
        '/api/sys': {
          target: env.VITE_API_URL_SYS,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api\/sys/, '/api'),
          secure: false,
          onProxyReq: (proxyReq, req, res) => {
            console.log('Proxying request:');
            console.log(`  Method: ${req.method}`);
            console.log(`  Path: ${proxyReq.path}`);
            console.log('  Request Headers:', req.headers);
          }
        },
        '/auth': {
          target: env.VITE_AUTH_URL,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/auth/, ''),
          secure: false,
          onProxyReq: (proxyReq, req, res) => {
            console.log('Proxying request:');
            console.log(`  Method: ${req.method}`);
            console.log(`  Path: ${proxyReq.path}`);
            console.log('  Request Headers:', req.headers);
          }
        },
        // 业务系统服务19903
        '/api/lmg': {
          target: env.VITE_API_URL_JKFZJC,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api\/lmg/, '/api'),
          secure: false,
          onProxyReq: (proxyReq, req, res) => {
            console.log('Proxying request:');
            console.log(`  Method: ${req.method}`);
            console.log(`  Path: ${proxyReq.path}`);
            console.log('  Request Headers:', req.headers);
          }
        },
        // 
        '/api/jkfzjc': {
          target: env.VITE_API_URL_JKFZJC,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api\/lmg/, '/api'),
          secure: false,
          onProxyReq: (proxyReq, req, res) => {
            console.log('Proxying request:');
            console.log(`  Method: ${req.method}`);
            console.log(`  Path: ${proxyReq.path}`);
            console.log('  Request Headers:', req.headers);
          }
        }
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          api: 'modern-compiler',
          additionalData: `
            @use "@/assets/scss/variables" as v;
            @use "@/assets/scss/mixins" as m;
          `
        }
      },
      postcss: {
        plugins: [
          postCssPxToRem({
            rootValue: 16,
            propList: ['*'],
            selectorBlackList: ['no-rem'],
            replace: true,
            mediaQuery: false,
            minPixelValue: 0
          }),
        ]
      }
    },
    define: {
      __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: JSON.stringify(false),
    },
    esbuild: {
      drop: ['console', 'debugger']
    }
  };
});
