// Copies rendered artifacts from remotion/out/ into apps/web/public/videos/
// where the marketing page expects them. Run after `pnpm render:all`.
//
// Kept as a plain mjs script (no deps) so it works without installing
// anything in the Web app workspace.

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

const __dirname = dirname(fileURLToPath(import.meta.url));
const SRC = resolve(__dirname, '..', 'out');
const DEST = resolve(__dirname, '..', '..', 'apps', 'web', 'public', 'videos');

const FILES = [
  { name: 'hero.mp4', maxBytes: 3 * 1024 * 1024 },
  { name: 'hero.webm', maxBytes: 3 * 1024 * 1024 },
  { name: 'hero-poster.jpg', maxBytes: 250 * 1024 },
];

mkdirSync(DEST, { recursive: true });

let missing = [];
for (const f of FILES) {
  const from = resolve(SRC, f.name);
  if (!existsSync(from)) {
    missing.push(f.name);
    continue;
  }
  const size = statSync(from).size;
  copyFileSync(from, resolve(DEST, f.name));
  const status = size > f.maxBytes ? '⚠ OVER LIMIT' : '✓';
  console.log(`${status}  ${f.name}  ${(size / 1024).toFixed(1)} KiB  (limit ${(f.maxBytes / 1024).toFixed(0)} KiB)`);
}

if (missing.length) {
  console.error(`\nMissing: ${missing.join(', ')} — run \`pnpm render:all\` first.`);
  process.exit(1);
}
