#!/usr/bin/env bash
# gsl-preview — Preview helper for gsl (called by fzf)
# Usage: gsl-preview <line-from-fzf>

LINE="$*"
SESSION_ID=$(echo "$LINE" | awk '{print $1}')

goose session export --session-id "$SESSION_ID" --format json 2>/dev/null | jq -r '
    "\n  \u001b[1m\(.name // "Unnamed")\u001b[0m  (\u001b[2m\(.message_count // 0) msgs\u001b[0m)\n  \u001b[2m\(.working_dir // "")\u001b[0m\n",
    (if (.conversation | length) > 0 then
        "\n  ─── First message ───",
        (.conversation[0] | .content | map(select(.type == "text")) | .[0].text // "(no text)" | .[0:500]),
        "\n  ─── Last message ───",
        (.conversation[-1] | .content | map(select(.type == "text")) | .[0].text // "(no text)" | .[0:500])
    else
        "  (empty session)"
    end)
' 2>/dev/null | head -30
