/**
 * gen-badges.js — 生成 6 张静态 SVG 二维码名牌 + 导出示例 CSV/JSON
 * 运行：node tests/gen-badges.js
 * 二维码内容 = 报名码本身（不是网址）。编码库为本地 vendor/qrcode.js（MIT）。
 */
'use strict';
const fs = require('fs');
const path = require('path');
const qrcode = require('../vendor/qrcode.js');

const OUT = path.join(__dirname, '..');
const SPEC = JSON.parse(fs.readFileSync(path.join(__dirname, '../../inputs/spec.json'), 'utf8'));

function badgeSvg(a, qrSvgInner) {
  return `<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="360" height="430" viewBox="0 0 360 430" font-family="-apple-system, 'PingFang SC', 'Microsoft YaHei', sans-serif">
  <rect width="360" height="430" rx="12" fill="#ffffff" stroke="#e5e7eb"/>
  <text x="180" y="34" text-anchor="middle" font-size="12" fill="#6b7280">青禾周末创作小聚 · 合成练习</text>
  <g transform="translate(100,50)">${qrSvgInner}</g>
  <text x="180" y="290" text-anchor="middle" font-size="26" font-weight="700" fill="#1f2937">${a.name}</text>
  <text x="180" y="322" text-anchor="middle" font-size="16" font-weight="600" fill="#2f6f4f">座位 ${a.seat} · ${a.id}</text>
  <text x="180" y="356" text-anchor="middle" font-size="18" letter-spacing="2" fill="#1f2937" font-family="monospace">${a.code}</text>
  <text x="180" y="392" text-anchor="middle" font-size="11" fill="#9ca3af">二维码内容为报名码 · 离线可验</text>
</svg>`;
}

const summary = [];
for (const a of SPEC.attendees) {
  const qr = qrcode(0, 'M');
  qr.addData(a.code);
  qr.make();
  const inner = qr.createSvgTag(4, 0).replace(/^<svg[^>]*>/, '').replace(/<\/svg>$/, '');
  const file = path.join(OUT, 'badges', `${a.id}.svg`);
  fs.writeFileSync(file, badgeSvg(a, inner), 'utf8');
  summary.push(`${a.id} ${a.name} ${a.seat} -> badges/${a.id}.svg (QR内容: ${a.code}, 模块数 ${qr.getModuleCount()})`);
}
console.log(summary.join('\n'));

// 导出示例（基于一个演示签到状态：A01/A02/A05 已签到）
const CheckinCore = require('../checkin-core.js');
const store = { _m: {}, getItem(k) { return this._m[k] || null; }, setItem(k, v) { this._m[k] = v; }, removeItem(k) { delete this._m[k]; } };
const app = CheckinCore.createApp(SPEC, store, { now: () => new Date('2026-11-28T09:15:00') });
app.checkIn('CF27-A01-K7', '手动输入');
app.checkIn(' cf27-a02-m3 ', '扫码');
app.checkIn('CF27-A05-T4', '手动输入');
fs.writeFileSync(path.join(OUT, 'exports', '签到记录-示例.csv'), app.exportCSV(), 'utf8');
fs.writeFileSync(path.join(OUT, 'exports', '签到记录-示例.json'), app.exportJSON(), 'utf8');
console.log('exports/签到记录-示例.csv 与 .json 已生成（演示状态：A01/A02/A05 已签到）');
