#!/bin/bash

# analyze_top_users.sh
# Analyzes the daily_completions table to find the top 10 anonymous IDs by completion count

# Set database path from argument or default to dev.db
DB_PATH="${1:-dev.db}"

# Check if database file exists
if [ ! -f "$DB_PATH" ]; then
    echo "Error: Database file not found: $DB_PATH"
    echo "Usage: $0 [database_path]"
    exit 1
fi

# Run the analysis query
sqlite3 "$DB_PATH" <<EOF
.mode column
.headers on
.width 36 16 16 17

SELECT
  anonymous_id,
  COUNT(*) as completion_count,
  MIN(date) as first_completion,
  MAX(date) as latest_completion
FROM daily_completions
GROUP BY anonymous_id
ORDER BY completion_count DESC
LIMIT 10;
EOF
