import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import fs from 'fs';
import { resolve } from 'path';

const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf8'));
const appVersion = packageJson.version;

// Get current date (this will be the server's local time where the build runs)
const now = new Date();

// Options for date formatting, targeting CET/CEST
const dateTimeFormatOptionsCEST = {
  year: 'numeric', month: '2-digit', day: '2-digit',
  hour: '2-digit', minute: '2-digit', second: '2-digit',
  hour12: false, // Use 24-hour format
  timeZone: 'Europe/Bratislava' // Or 'Europe/Prague', 'Europe/Berlin', etc. (any major CET/CEST city)
  // This will automatically handle Daylight Saving Time (CEST vs CET)
};

// Generate build time string using a specific time zone that observes CET/CEST
const appBuildTime = now.toLocaleString('sk-SK', dateTimeFormatOptionsCEST) + " CEST/CET"; // Add timezone indicator for clarity


export default defineConfig({
  plugins: [vue()],
  server: {
    port: 8080
  },
  define: {
    'import.meta.env.VITE_APP_BUILD_TIME': JSON.stringify(appBuildTime),
    '__APP_CACHE_VERSION__': JSON.stringify(`nexus-timer-cache-v${appVersion}-${Date.now()}`)
  },
  build: {
    rollupOptions: {
      input: {
        main: resolve(__dirname, 'index.html'),
        sw: resolve(__dirname, 'src/sw.js')
      },
      output: {
        entryFileNames: assetInfo => {
          if (assetInfo.name === 'sw') {
            return 'service-worker.js';
          }
          return 'assets/[name]-[hash].js';
        },
        chunkFileNames: 'assets/[name]-[hash].js',
        assetFileNames: 'assets/[name]-[hash].[ext]',
      }
    },
    emptyOutDir: true, 
  }
});