/**
 * After Vite builds the dashboard, copy the dist/ output into
 * @shade/observer's dist/ directory so the observer endpoint can
 * serve it from /dashboard/.
 */
import { existsSync, mkdirSync, cpSync, rmSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';

const here = dirname(fileURLToPath(import.meta.url));
const dashboardDist = join(here, '..', 'dist');
const observerDist = join(here, '..', '..', 'shade-observer', 'dist');

if (!existsSync(dashboardDist)) {
  console.error(`Dashboard dist not found at ${dashboardDist}. Run \`vite build\` first.`);
  process.exit(1);
}

// Clean and recreate observer dist
if (existsSync(observerDist)) {
  rmSync(observerDist, { recursive: true });
}
mkdirSync(observerDist, { recursive: true });

cpSync(dashboardDist, observerDist, { recursive: true });
console.log(`✓ Copied dashboard build to ${observerDist}`);
