export async function syncToTrello(
  cfg: { apiKey: string; token: string; listId: string },
  fields: { title: string; description: string },
): Promise<{ external_id: string; external_url: string | null }> {
  const params = new URLSearchParams({
    idList: cfg.listId,
    name: fields.title.slice(0, 160),
    desc: fields.description.slice(0, 4000),
    key: cfg.apiKey,
    token: cfg.token,
  });
  const res = await fetch(`https://api.trello.com/1/cards?${params}`, { method: 'POST' });
  if (!res.ok) throw new Error(`Trello HTTP ${res.status}: ${(await res.text()).slice(0, 200)}`);
  const card = await res.json() as { id: string; shortUrl?: string; url?: string };
  return { external_id: card.id, external_url: card.shortUrl || card.url || null };
}
