import { readdirSync, mkdirSync, copyFileSync } from 'node:fs';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';

// tsc only emits .ts; non-TS assets read at runtime via import.meta.url must be
// copied into dist/ so the compiled build can find them.
function copyDir(src, dest) {
  mkdirSync(dest, { recursive: true });
  for (const entry of readdirSync(src, { withFileTypes: true })) {
    const from = join(src, entry.name);
    const to = join(dest, entry.name);
    if (entry.isDirectory()) {
      copyDir(from, to);
    } else {
      copyFileSync(from, to);
    }
  }
}

const backendRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..');
const to = resolve(backendRoot, 'dist/services/email/htmlTemplates');
copyDir(resolve(backendRoot, 'src/services/email/htmlTemplates'), to);
console.log(`Copied email templates -> ${to}`);
