import * as esbuild from 'esbuild';
import { NovadiUnplugin } from '@novadi/core/unplugin';
import * as fs from 'fs';
import * as path from 'path';

const entryPoints = [
  'src/index.ts',
  'src/extensions/teams/index.ts',
  'src/extensions/departments/index.ts',
  'src/extensions/bookings/index.ts',
  'src/extensions/customers/index.ts',
  'src/extensions/schedules/index.ts',
  'src/extensions/audit/index.ts'
];

async function build() {
  await esbuild.build({
    entryPoints,
    bundle: true,
    outdir: 'dist',
    format: 'esm',
    platform: 'browser',
    external: ['@novadi/core', 'dayjs'],
    splitting: true,
    sourcemap: true,
    target: 'es2020',
    plugins: [NovadiUnplugin.esbuild({ debug: false, enableAutowiring: true })]
  });

  console.log('Build complete: dist/');

  // Bundle CSS
  const cssDir = 'dist/css';
  if (!fs.existsSync(cssDir)) {
    fs.mkdirSync(cssDir, { recursive: true });
  }
  const cssFiles = [
    '../../wwwroot/css/calendar-base.css',
    '../../wwwroot/css/calendar-layout.css',
    '../../wwwroot/css/calendar-events.css'
  ];
  const bundledCss = cssFiles.map(f => fs.readFileSync(f, 'utf8')).join('\n');
  fs.writeFileSync(path.join(cssDir, 'calendar.css'), bundledCss);
  console.log('CSS bundled: dist/css/calendar.css');
}

build();
