-- name: UpsertSummary :one
INSERT INTO commit_summaries (
  id, workspace_id, worktree_id, branch, commit_sha, kind,
  title, body_md, model, status, error
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
ON CONFLICT (workspace_id, commit_sha, kind) DO UPDATE
SET title = EXCLUDED.title,
    body_md = EXCLUDED.body_md,
    model = EXCLUDED.model,
    status = EXCLUDED.status,
    error = EXCLUDED.error,
    branch = EXCLUDED.branch,
    worktree_id = EXCLUDED.worktree_id
RETURNING id, workspace_id, worktree_id, branch, commit_sha, kind,
          title, body_md, model, status, error, created_at;

-- name: GetSummary :one
SELECT id, workspace_id, worktree_id, branch, commit_sha, kind,
       title, body_md, model, status, error, created_at
FROM commit_summaries
WHERE workspace_id = $1 AND commit_sha = $2 AND kind = $3;

-- name: InsertPendingSummary :one
INSERT INTO commit_summaries (
  id, workspace_id, worktree_id, branch, commit_sha, kind, body_md, status
) VALUES ($1, $2, $3, $4, $5, $6, '', 'pending')
ON CONFLICT (workspace_id, commit_sha, kind) DO NOTHING
RETURNING id, workspace_id, worktree_id, branch, commit_sha, kind,
          title, body_md, model, status, error, created_at;
