import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

const __dirname = path.dirname(fileURLToPath(import.meta.url));
const catalogPath = path.join(__dirname, '../lib/emoji-catalog.ts');
const src = fs.readFileSync(catalogPath, 'utf8');
const ids = [...src.matchAll(/id: '(e-[^']+)'/g)].map((match) => match[1]);

function face(id, mouth, extra = '') {
  return `<symbol id="${id}" viewBox="0 0 36 36"><circle cx="18" cy="18" r="16" fill="#FFCC4D"/><ellipse cx="12" cy="15" rx="2.2" ry="3" fill="#664500"/><ellipse cx="24" cy="15" rx="2.2" ry="3" fill="#664500"/>${mouth}${extra}</symbol>`;
}

function heart(id, color) {
  return `<symbol id="${id}" viewBox="0 0 36 36"><path d="M18 30s-10-6.5-10-14a5.5 5.5 0 0 1 10-3 5.5 5.5 0 0 1 10 3c0 7.5-10 14-10 14z" fill="${color}"/></symbol>`;
}

function circle(id, color, label) {
  return `<symbol id="${id}" viewBox="0 0 36 36"><circle cx="18" cy="18" r="14" fill="${color}"/><text x="18" y="22" text-anchor="middle" font-size="10" fill="#fff" font-family="Arial,sans-serif">${label}</text></symbol>`;
}

const mouthSmile =
  '<path d="M10 21c2.5 3 13.5 3 16 0" stroke="#664500" stroke-width="2" fill="none" stroke-linecap="round"/>';
const mouthLaugh = '<path d="M10 19c2 5 14 5 16 0" fill="#664500"/>';
const mouthSad =
  '<path d="M10 24c2.5-3 13.5-3 16 0" stroke="#664500" stroke-width="2" fill="none" stroke-linecap="round"/>';
const mouthOpen = '<ellipse cx="18" cy="22" rx="5" ry="4" fill="#664500"/>';
const mouthLine = '<path d="M12 22h12" stroke="#664500" stroke-width="2" stroke-linecap="round"/>';

const parts = ['<svg xmlns="http://www.w3.org/2000/svg" style="display:none">'];

for (const id of ids) {
  if (id.includes('grinning') || id.includes('smile') || id.includes('blush') || id.includes('slight')) {
    parts.push(face(id, mouthSmile));
  } else if (id.includes('laugh') || id.includes('rofl') || id.includes('party')) {
    parts.push(face(id, mouthLaugh));
  } else if (id.includes('cry') || id.includes('sob') || id.includes('tired')) {
    parts.push(face(id, mouthSad));
  } else if (id.includes('shock') || id.includes('fear') || id.includes('rage') || id.includes('angry') || id.includes('sick') || id.includes('mask')) {
    parts.push(face(id, mouthOpen));
  } else if (id.includes('heart')) {
    const colors = {
      red: '#EF4444',
      orange: '#F97316',
      yellow: '#EAB308',
      green: '#22C55E',
      blue: '#3B82F6',
      purple: '#A855F7',
      black: '#111827',
      white: '#F9FAFB',
      broken: '#9CA3AF',
      sparkling: '#EC4899',
      two: '#F43F5E',
      revolving: '#FB7185'
    };
    const key = Object.keys(colors).find((item) => id.includes(item)) || 'red';
    parts.push(heart(id, colors[key]));
  } else if (id.includes('thumbs-up')) {
    parts.push(`<symbol id="${id}" viewBox="0 0 36 36"><rect x="10" y="8" width="8" height="18" rx="4" fill="#FBBF24" transform="rotate(-20 14 17)"/><rect x="18" y="14" width="10" height="8" rx="3" fill="#FBBF24"/></symbol>`);
  } else if (id.includes('thumbs-down')) {
    parts.push(`<symbol id="${id}" viewBox="0 0 36 36"><rect x="10" y="10" width="8" height="18" rx="4" fill="#FBBF24" transform="rotate(20 14 19)"/><rect x="18" y="14" width="10" height="8" rx="3" fill="#FBBF24"/></symbol>`);
  } else if (id.includes('fire')) {
    parts.push(`<symbol id="${id}" viewBox="0 0 36 36"><path d="M18 4c2 6 6 8 6 14a6 6 0 1 1-12 0c0-4 3-6 6-14z" fill="#F97316"/><path d="M18 14c1 3 3 4 3 7a3 3 0 1 1-6 0c0-2 1-3 3-7z" fill="#FACC15"/></symbol>`);
  } else if (id.includes('star')) {
    parts.push(`<symbol id="${id}" viewBox="0 0 36 36"><polygon points="18,3 22,14 34,14 24,21 28,33 18,26 8,33 12,21 2,14 14,14" fill="#FACC15"/></symbol>`);
  } else if (id.includes('check')) {
    parts.push(`<symbol id="${id}" viewBox="0 0 36 36"><circle cx="18" cy="18" r="16" fill="#22C55E"/><path d="M11 18l4 4 10-10" stroke="#fff" stroke-width="3" fill="none" stroke-linecap="round"/></symbol>`);
  } else if (id.includes('cross')) {
    parts.push(`<symbol id="${id}" viewBox="0 0 36 36"><circle cx="18" cy="18" r="16" fill="#EF4444"/><path d="M13 13l10 10M23 13l-10 10" stroke="#fff" stroke-width="3" stroke-linecap="round"/></symbol>`);
  } else if (id.includes('lock')) {
    parts.push(`<symbol id="${id}" viewBox="0 0 36 36"><rect x="10" y="16" width="16" height="14" rx="3" fill="#64748B"/><path d="M13 16v-3a5 5 0 0 1 10 0v3" stroke="#64748B" stroke-width="3" fill="none"/></symbol>`);
  } else if (id.includes('dog')) {
    parts.push(`<symbol id="${id}" viewBox="0 0 36 36"><circle cx="18" cy="20" r="10" fill="#D97706"/><circle cx="10" cy="12" r="4" fill="#92400E"/><circle cx="26" cy="12" r="4" fill="#92400E"/></symbol>`);
  } else if (id.includes('cat')) {
    parts.push(`<symbol id="${id}" viewBox="0 0 36 36"><circle cx="18" cy="20" r="10" fill="#FB923C"/><polygon points="10,12 14,18 6,18" fill="#FB923C"/><polygon points="26,12 30,18 22,18" fill="#FB923C"/></symbol>`);
  } else if (id.includes('pizza')) {
    parts.push(`<symbol id="${id}" viewBox="0 0 36 36"><path d="M6 28 L30 28 L18 6 Z" fill="#FACC15"/><circle cx="16" cy="22" r="2" fill="#EF4444"/></symbol>`);
  } else if (id.includes('coffee')) {
    parts.push(`<symbol id="${id}" viewBox="0 0 36 36"><rect x="10" y="12" width="14" height="12" rx="3" fill="#78350F"/><path d="M24 15h3a3 3 0 0 1 0 6h-3" stroke="#78350F" stroke-width="2" fill="none"/></symbol>`);
  } else if (id.includes('car')) {
    parts.push(`<symbol id="${id}" viewBox="0 0 36 36"><rect x="6" y="16" width="24" height="10" rx="4" fill="#3B82F6"/><circle cx="12" cy="26" r="3" fill="#111"/><circle cx="24" cy="26" r="3" fill="#111"/></symbol>`);
  } else if (id.includes('airplane')) {
    parts.push(`<symbol id="${id}" viewBox="0 0 36 36"><path d="M6 18 L30 10 L22 18 L30 26 Z" fill="#60A5FA"/></symbol>`);
  } else if (id.includes('rocket')) {
    parts.push(`<symbol id="${id}" viewBox="0 0 36 36"><path d="M18 4c4 8 4 12 4 18H14c0-6 0-10 4-18z" fill="#CBD5E1"/><circle cx="18" cy="16" r="3" fill="#38BDF8"/></symbol>`);
  } else if (id.includes('bulb')) {
    parts.push(`<symbol id="${id}" viewBox="0 0 36 36"><circle cx="18" cy="16" r="9" fill="#FDE047"/><rect x="14" y="24" width="8" height="4" rx="2" fill="#94A3B8"/></symbol>`);
  } else if (id.includes('phone')) {
    parts.push(`<symbol id="${id}" viewBox="0 0 36 36"><rect x="12" y="6" width="12" height="24" rx="3" fill="#334155"/></symbol>`);
  } else if (id.includes('100')) {
    parts.push(`<symbol id="${id}" viewBox="0 0 36 36"><rect x="4" y="8" width="28" height="20" rx="4" fill="#EF4444"/><text x="18" y="22" text-anchor="middle" font-size="10" fill="#fff" font-family="Arial,sans-serif">100</text></symbol>`);
  } else if (id.includes('wink')) {
    parts.push(face(id, mouthSmile, '<path d="M22 15h4" stroke="#664500" stroke-width="2" stroke-linecap="round"/>'));
  } else if (id.includes('thinking')) {
    parts.push(face(id, mouthLine, '<path d="M24 8c3 0 3 4 0 4" stroke="#664500" stroke-width="2" fill="none"/>'));
  } else if (id.includes('sleeping')) {
    parts.push(face(id, mouthLine, '<text x="24" y="10" font-size="8" fill="#3B82F6" font-family="Arial">Zzz</text>'));
  } else if (id.includes('cool')) {
    parts.push(face(id, mouthSmile, '<rect x="8" y="13" width="20" height="5" rx="2" fill="#111" opacity=".85"/>'));
  } else if (id.includes('kiss')) {
    parts.push(face(id, '', '<circle cx="18" cy="22" r="4" fill="#EC4899"/>'));
  } else if (id.includes('tongue')) {
    parts.push(face(id, '', '<ellipse cx="18" cy="21" rx="4" ry="3" fill="#664500"/><ellipse cx="18" cy="26" rx="3" ry="4" fill="#F472B6"/>'));
  } else if (id.includes('wave')) {
    parts.push(`<symbol id="${id}" viewBox="0 0 36 36"><path d="M8 20c0-8 8-8 8 0s8-8 8 0" stroke="#FBBF24" stroke-width="5" fill="none" stroke-linecap="round"/></symbol>`);
  } else if (id.includes('clap')) {
    parts.push(`<symbol id="${id}" viewBox="0 0 36 36"><rect x="8" y="10" width="7" height="16" rx="3" fill="#FBBF24"/><rect x="21" y="10" width="7" height="16" rx="3" fill="#FBBF24"/></symbol>`);
  } else {
    const label = id.replace('e-', '').slice(0, 3).toUpperCase();
    const hue = Math.abs(id.split('').reduce((acc, char) => acc + char.charCodeAt(0), 0)) % 360;
    parts.push(circle(id, `hsl(${hue} 70% 55%)`, label));
  }
}

parts.push('</svg>');
const out = path.join(__dirname, '../public/emojis/lendry-emojis.svg');
fs.mkdirSync(path.dirname(out), { recursive: true });
fs.writeFileSync(out, parts.join(''));
console.log(`Generated ${ids.length} emoji symbols`);
