import fs from 'fs';
import path from 'path';
import type { Plugin } from 'vite';
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import tailwindcss from '@tailwindcss/vite';
import basicSsl from '@vitejs/plugin-basic-ssl';

/** http-local: http://localhost:3000 | https-lan: https://{LAN IP}:3000 */
function readLanIp(): string | null {
  const file = path.join(__dirname, '.lan-ip');
  if (!fs.existsSync(file)) return null;
  const ip = fs.readFileSync(file, 'utf8').trim();
  return ip || null;
}

function lanOnlyUrls(lanHost: string, port: number): Plugin {
  return {
    name: 'lan-only-urls',
    configureServer(server) {
      server.printUrls = () => {
        server.config.logger.info(`\n  ➜  LAN:  https://${lanHost}:${port}/\n`, { clear: true });
      };
    },
  };
}

export default defineConfig(({ mode }) => {
  const isHttpsLan = mode === 'https-lan';
  const lanHost = readLanIp();

  if (isHttpsLan && !lanHost) {
    throw new Error('frontend/.lan-ip missing — restart via 서버시작.bat');
  }

  return {
    plugins: [
      react(),
      tailwindcss(),
      ...(isHttpsLan ? [basicSsl(), lanOnlyUrls(lanHost!, 3000)] : []),
    ],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
      },
    },
    server: {
      port: 3000,
      strictPort: true,
      host: isHttpsLan ? lanHost! : 'localhost',
      https: isHttpsLan,
      proxy: {
        '/api': {
          target: 'http://localhost:4000',
          changeOrigin: true,
        },
        '/uploads': {
          target: 'http://localhost:4000',
          changeOrigin: true,
        },
        '/socket.io': {
          target: 'http://localhost:4000',
          changeOrigin: true,
          ws: true,
        },
      },
    },
  };
});
