#!/bin/bash
# Check PostHog dashboard insights
# Usage: ./check-dashboard.sh [PROJECT_ID] [DASHBOARD_ID]

POSTHOG_KEY=$(cat ~/.config/posthog/api-key 2>/dev/null)
if [ -z "$POSTHOG_KEY" ]; then
  echo "Error: PostHog API key not found at ~/.config/posthog/api-key"
  exit 1
fi

PROJECT_ID=${1:-316683}
DASHBOARD_ID=$2

if [ -z "$DASHBOARD_ID" ]; then
  echo "Listing all dashboards for project $PROJECT_ID..."
  curl -s -H "Authorization: Bearer $POSTHOG_KEY" \
    "https://us.posthog.com/api/projects/$PROJECT_ID/dashboards/" | \
    jq '.results[] | {id, name}'
else
  echo "Checking dashboard $DASHBOARD_ID insights..."
  curl -s -H "Authorization: Bearer $POSTHOG_KEY" \
    "https://us.posthog.com/api/projects/$PROJECT_ID/dashboards/$DASHBOARD_ID/" | \
    jq '.tiles[] | {
      insight_id: .insight.id,
      name: .insight.name,
      query_kind: .insight.query.kind,
      source_kind: .insight.query.source.kind,
      event: .insight.query.source.series[0].event,
      display: .insight.query.source.trendsFilter.display
    }'
fi
