#!/usr/bin/env bun
/**
 * Copies font files from @fontsource packages into ui/fonts/ for offline use.
 * Run before Tauri build so the app works without internet.
 */
const fs = require('fs');
const path = require('path');

const ROOT = path.resolve(__dirname, '..');
const UI_FONTS = path.join(ROOT, 'ui', 'fonts');

const FONTS = [
  // Bebas Neue – lock title, brand
  { pkg: '@fontsource/bebas-neue', files: ['bebas-neue-latin-400-normal.woff2'] },
  // Rajdhani – lock screen, labels (400, 500, 600, 700)
  {
    pkg: '@fontsource/rajdhani',
    files: [400, 500, 600, 700].map((w) => `rajdhani-latin-${w}-normal.woff2`),
  },
  // IBM Plex Sans – body text (500, 600)
  {
    pkg: '@fontsource/ibm-plex-sans',
    files: ['ibm-plex-sans-latin-500-normal.woff2', 'ibm-plex-sans-latin-600-normal.woff2'],
  },
];

fs.mkdirSync(UI_FONTS, { recursive: true });

for (const { pkg, files } of FONTS) {
  const srcDir = path.join(ROOT, 'node_modules', pkg, 'files');
  if (!fs.existsSync(srcDir)) {
    console.warn(`Skipping ${pkg}: not installed. Run bun install.`);
    continue;
  }
  for (const file of files) {
    const src = path.join(srcDir, file);
    const dest = path.join(UI_FONTS, file);
    if (fs.existsSync(src)) {
      fs.copyFileSync(src, dest);
      console.log('Copied', file);
    } else {
      console.warn('Missing', src);
    }
  }
}

console.log('Fonts copied to ui/fonts/');
