export async function syncToGitHub(
  cfg: { token: string; owner: string; repo: string },
  fields: { title: string; description: string },
): Promise<{ external_id: string; external_url: string | null }> {
  const res = await fetch(`https://api.github.com/repos/${cfg.owner}/${cfg.repo}/issues`, {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${cfg.token}`,
      Accept: 'application/vnd.github+json',
      'Content-Type': 'application/json',
      'X-GitHub-Api-Version': '2022-11-28',
    },
    body: JSON.stringify({
      title: fields.title.slice(0, 200),
      body: fields.description.slice(0, 6000),
    }),
  });
  if (!res.ok) throw new Error(`GitHub HTTP ${res.status}: ${(await res.text()).slice(0, 240)}`);
  const issue = await res.json() as { number: number; html_url?: string };
  return {
    external_id: String(issue.number),
    external_url: issue.html_url ?? null,
  };
}
