#!/usr/bin/env node
/**
 * MCP Animation Frame Generator
 * Usage: node generate.js configs/your-config.json
 */

import fs from 'fs';
import path from 'path';

const configPath = process.argv[2];
if (!configPath) {
  console.error('Usage: node generate.js configs/your-config.json');
  process.exit(1);
}

const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
const outputDir = path.join('output', config.name);

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

// Generate panel HTML based on type
function generatePanel(panel, index) {
  const gridSpan = panel.gridSpan === 2 ? 'grid-row: span 2;' : '';
  
  let content = '';
  
  switch (panel.type) {
    case 'list':
      content = generateListPanel(panel);
      break;
    case 'stats':
      content = generateStatsPanel(panel);
      break;
    case 'chart':
      content = generateChartPanel(panel);
      break;
    case 'chat':
      content = generateChatPanel(panel);
      break;
    default:
      content = '<div class="panel-content">Unknown panel type</div>';
  }
  
  return `
    <div class="embed-panel" style="${gridSpan}">
      <div class="panel-header">
        <div class="panel-icon" style="background: ${panel.iconColor};">${panel.icon}</div>
        <div class="panel-title">${panel.title}</div>
      </div>
      <div class="panel-content">
        ${content}
      </div>
    </div>
  `;
}

function generateListPanel(panel) {
  const items = panel.items.map(item => `
    <div class="list-item">
      <div class="item-avatar">${item.avatar}</div>
      <div class="item-name">${item.name}</div>
      <div class="item-status" style="background: ${item.statusColor};">${item.status}</div>
      <div class="item-meta">${item.meta}</div>
    </div>
  `).join('');
  
  return `<div class="item-list">${items}</div>`;
}

function generateStatsPanel(panel) {
  const stats = panel.stats.map(stat => `
    <div class="stat-row">
      <span class="stat-label">${stat.label}</span>
      <span class="stat-value">${stat.value}</span>
      ${stat.change ? `<span class="stat-change positive">${stat.change}</span>` : ''}
    </div>
  `).join('');
  
  const highlight = panel.highlight ? `
    <div class="stat-highlight" style="border-color: ${panel.highlight.color};">
      <div class="highlight-label">${panel.highlight.label}</div>
      <div class="highlight-value" style="color: ${panel.highlight.color};">${panel.highlight.value}</div>
    </div>
  ` : '';
  
  return `<div class="stats-grid">${stats}</div>${highlight}`;
}

function generateChartPanel(panel) {
  return `
    <div class="chart-content">
      <div class="donut" style="background: conic-gradient(#6366f1 0% ${panel.value}%, #2a2a4a ${panel.value}% 100%);">
        <div class="donut-inner">${panel.centerLabel}</div>
      </div>
      <div class="chart-label">${panel.bottomValue}</div>
      <div class="chart-sublabel">${panel.bottomLabel}</div>
    </div>
  `;
}

function generateChatPanel(panel) {
  const messages = panel.messages.map(msg => `
    <div class="mini-msg ${msg.direction}">${msg.text}</div>
  `).join('');
  
  return `<div class="mini-chat">${messages}</div>`;
}

// Base HTML template
function generateHTML(config, frameType) {
  const showUserMessage = ['first-exchange', 'typing-second', 'loading', 'final'].includes(frameType);
  const showAiResponse = ['first-exchange', 'typing-second', 'loading', 'final'].includes(frameType);
  const showPanels = ['loading', 'final'].includes(frameType);
  const showLoading = frameType === 'loading';
  const showTyping = frameType === 'typing' || frameType === 'typing-second';
  
  const panels = showPanels ? config.panels.map((p, i) => generatePanel(p, i)).join('') : '';
  
  return `<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=${config.dimensions.width}, height=${config.dimensions.height}">
  <title>${config.title}</title>
  <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
  <style>
    * { margin: 0; padding: 0; box-sizing: border-box; }
    body {
      font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
      background: #0f0f1a;
      width: ${config.dimensions.width}px;
      height: ${config.dimensions.height}px;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .window {
      width: 1600px;
      height: 900px;
      background: #1a1a2e;
      border-radius: 12px;
      box-shadow: 0 25px 80px rgba(0,0,0,0.5);
      display: flex;
      flex-direction: column;
      border: 1px solid #2a2a4a;
    }
    .titlebar {
      height: 48px;
      background: #252540;
      display: flex;
      align-items: center;
      padding: 0 16px;
      border-bottom: 1px solid #2a2a4a;
      flex-shrink: 0;
    }
    .traffic-lights { display: flex; gap: 8px; }
    .light { width: 12px; height: 12px; border-radius: 50%; }
    .light.red { background: #ff5f57; }
    .light.yellow { background: #ffbd2e; }
    .light.green { background: #28ca41; }
    .title { flex: 1; text-align: center; color: #8888aa; font-size: 14px; font-weight: 500; }
    .chat-area { flex: 1; padding: 20px; display: flex; flex-direction: column; gap: 12px; overflow: hidden; }
    .message { display: flex; flex-direction: column; max-width: 85%; }
    .message.user { align-self: flex-end; }
    .message.ai { align-self: flex-start; width: 100%; max-width: 100%; }
    .bubble { padding: 16px 20px; border-radius: 18px; font-size: 16px; line-height: 1.5; }
    .message.user .bubble {
      background: linear-gradient(135deg, #6366f1, #8b5cf6);
      color: white;
      border-bottom-right-radius: 6px;
    }
    .message.ai .bubble.text {
      background: #2a2a4a;
      color: #e2e2f0;
      border-bottom-left-radius: 6px;
      width: fit-content;
    }
    .typing-indicator {
      display: flex;
      gap: 4px;
      padding: 16px 20px;
      background: #2a2a4a;
      border-radius: 18px;
      width: fit-content;
    }
    .typing-dot {
      width: 8px;
      height: 8px;
      background: #6366f1;
      border-radius: 50%;
      animation: bounce 1.4s infinite ease-in-out;
    }
    .typing-dot:nth-child(2) { animation-delay: 0.2s; }
    .typing-dot:nth-child(3) { animation-delay: 0.4s; }
    @keyframes bounce {
      0%, 80%, 100% { transform: translateY(0); }
      40% { transform: translateY(-6px); }
    }
    .embed-grid {
      margin-top: 8px;
      display: grid;
      grid-template-columns: 1fr 1fr 1fr;
      grid-template-rows: 1fr 1fr;
      height: 510px;
      gap: 4px;
      background: #2a2a4a;
      border-radius: 12px;
      overflow: hidden;
    }
    .embed-panel {
      background: #12121f;
      display: flex;
      flex-direction: column;
      padding: 20px;
      overflow: hidden;
    }
    .panel-header {
      display: flex;
      align-items: center;
      gap: 12px;
      margin-bottom: 16px;
      padding-bottom: 12px;
      border-bottom: 1px solid #2a2a4a;
    }
    .panel-icon {
      width: 32px;
      height: 32px;
      border-radius: 8px;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 13px;
      font-weight: 700;
      color: white;
    }
    .panel-title { font-size: 16px; font-weight: 600; color: #e2e2f0; }
    .panel-content { flex: 1; overflow: hidden; }
    
    /* List Items */
    .item-list { display: flex; flex-direction: column; gap: 8px; }
    .list-item {
      display: flex;
      align-items: center;
      gap: 10px;
      padding: 10px 12px;
      background: #1a1a2e;
      border-radius: 8px;
      font-size: 13px;
    }
    .item-avatar {
      width: 32px;
      height: 32px;
      border-radius: 50%;
      background: #3a3a5a;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 11px;
      font-weight: 600;
      color: #8888aa;
    }
    .item-name { color: #e2e2f0; flex: 1; font-weight: 500; }
    .item-status { padding: 4px 8px; border-radius: 5px; font-size: 10px; font-weight: 600; color: white; text-transform: uppercase; }
    .item-meta { color: #8888aa; font-size: 12px; font-weight: 500; }
    
    /* Stats */
    .stats-grid { display: flex; flex-direction: column; gap: 12px; }
    .stat-row { display: flex; align-items: center; padding: 12px; background: #1a1a2e; border-radius: 8px; }
    .stat-label { flex: 1; color: #8888aa; font-size: 13px; }
    .stat-value { color: #e2e2f0; font-weight: 600; font-size: 14px; margin-right: 8px; }
    .stat-change { color: #22c55e; font-size: 12px; font-weight: 600; }
    .stat-highlight { margin-top: 16px; padding: 16px; background: #1a1a2e; border-radius: 10px; text-align: center; border: 1px solid; }
    .highlight-label { color: #8888aa; font-size: 12px; }
    .highlight-value { font-size: 28px; font-weight: 700; margin-top: 4px; }
    
    /* Charts */
    .chart-content { display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100%; }
    .donut {
      width: 120px;
      height: 120px;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .donut-inner {
      width: 80px;
      height: 80px;
      background: #12121f;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 20px;
      font-weight: 700;
      color: #6366f1;
    }
    .chart-label { margin-top: 16px; font-size: 18px; font-weight: 600; color: #e2e2f0; }
    .chart-sublabel { font-size: 13px; color: #8888aa; margin-top: 4px; }
    
    /* Loading Skeleton */
    .skeleton { background: linear-gradient(90deg, #2a2a4a 25%, #3a3a5a 50%, #2a2a4a 75%); background-size: 200% 100%; animation: shimmer 1.5s infinite; border-radius: 6px; }
    @keyframes shimmer { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }
    
    .input-area { padding: 12px 20px 20px; border-top: 1px solid #2a2a4a; flex-shrink: 0; }
    .input-wrapper {
      display: flex;
      gap: 12px;
      align-items: center;
      background: #12121f;
      border: 1px solid #2a2a4a;
      border-radius: 12px;
      padding: 10px 14px;
    }
    .input-field { flex: 1; color: #5a5a7a; font-size: 13px; }
    .send-btn {
      width: 32px;
      height: 32px;
      background: linear-gradient(135deg, #6366f1, #8b5cf6);
      border-radius: 8px;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    .send-btn svg { width: 16px; height: 16px; color: white; }
  </style>
</head>
<body>
  <div class="window">
    <div class="titlebar">
      <div class="traffic-lights">
        <div class="light red"></div>
        <div class="light yellow"></div>
        <div class="light green"></div>
      </div>
      <div class="title">${config.title}</div>
      <div style="width: 52px;"></div>
    </div>
    <div class="chat-area">
      ${showUserMessage ? `
      <div class="message user">
        <div class="bubble">${config.userPrompt}</div>
      </div>
      ` : ''}
      ${showTyping && !showAiResponse ? `
      <div class="message ai">
        <div class="typing-indicator">
          <div class="typing-dot"></div>
          <div class="typing-dot"></div>
          <div class="typing-dot"></div>
        </div>
      </div>
      ` : ''}
      ${showAiResponse ? `
      <div class="message ai">
        <div class="bubble text">${config.aiResponse}</div>
        ${showPanels ? `<div class="embed-grid">${panels}</div>` : ''}
      </div>
      ` : ''}
    </div>
    <div class="input-area">
      <div class="input-wrapper">
        <span class="input-field">Type a message...</span>
        <div class="send-btn">
          <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <path d="M22 2L11 13M22 2L15 22L11 13L2 9L22 2Z"/>
          </svg>
        </div>
      </div>
    </div>
  </div>
</body>
</html>`;
}

// Generate all frames
const frames = [
  { name: 'frame-01-empty', type: 'empty' },
  { name: 'frame-02-typing', type: 'typing' },
  { name: 'frame-03-first-exchange', type: 'first-exchange' },
  { name: 'frame-04-typing-second', type: 'typing-second' },
  { name: 'frame-05-loading', type: 'loading' },
  { name: 'frame-06-final', type: 'final' }
];

frames.forEach(frame => {
  const html = generateHTML(config, frame.type);
  const filePath = path.join(outputDir, `${frame.name}.html`);
  fs.writeFileSync(filePath, html);
  console.log(`Generated: ${filePath}`);
});

console.log(`\n✓ Generated ${frames.length} frames in ${outputDir}/`);
console.log(`\nTo preview: open ${outputDir}/frame-06-final.html`);
console.log(`To export: use browser screenshot or puppeteer`);
