#!/usr/bin/env bash
# A short tour of fantasy-sports against your own league. Read-only.
#
#   PAUSE=3 LEAGUE=my-league ./demo.sh
#
# Needs fantasy-sports on PATH (uv tool install git+https://github.com/jwulff/fantasy-sports),
# jq, and a league configured in ~/.config/fantasy-sports/config.toml. Pass
# TEAM="Your Team Name" for the roster step; it defaults to the first team.
set -uo pipefail
PAUSE="${PAUSE:-2}"
LEAGUE_FLAG=()
[[ -n "${LEAGUE:-}" ]] && LEAGUE_FLAG=(--league "$LEAGUE")
FS="fantasy-sports"

show() { printf '\n\033[1;32m$\033[0m \033[1m%s\033[0m\n' "$1"; }
pause() { sleep "$PAUSE"; }

show "fantasy-sports doctor | jq -r '.data.checks[] | \"\\(.status)\\t\\(.name)\\t\\(.summary)\"'"
$FS doctor | jq -r '.data.checks[] | select(.status != "skipped") | "\(.status)\t\(.name)\t\(.summary)"' | column -t -s $'\t'
pause

show "fantasy-sports teams --no-raw | jq -r '.data[] | \"\\(.name)\\t\\(.owner_names[0])\"'"
$FS teams "${LEAGUE_FLAG[@]}" --no-raw | jq -r '.data[] | "\(.name)\t\(.owner_names[0])"' | column -t -s $'\t'
pause

TEAM="${TEAM:-$($FS teams "${LEAGUE_FLAG[@]}" --no-raw | jq -r '.data[0].name')}"
show "fantasy-sports roster --team \"$TEAM\" --no-raw | jq -r '...starters, with kickoff...'"
$FS roster "${LEAGUE_FLAG[@]}" --team "$TEAM" --no-raw \
  | jq -r '.data[] | select(.is_starter)
           | "\(.slot)\t\(.player.name)\t\(.player.pro_team)\tproj \(.player.projected_points)\t\(.player.kickoff)"' \
  | column -t -s $'\t'
pause

WEEK="${WEEK:-1}"
show "fantasy-sports box-scores --week $WEEK --no-raw | jq -r '...players who have scored, best first...'"
$FS box-scores "${LEAGUE_FLAG[@]}" --week "$WEEK" --no-raw \
  | jq -r '[.data[] | (.team_a_lineup + .team_b_lineup)[] | select(.actual_points > 0)]
           | sort_by(-.actual_points)[:8][]
           | "\(.player_name)\t\(.position)\t\(.pro_team) v \(.pro_opponent)\tproj \(.projected_points)\tactual \(.actual_points)"' \
  | column -t -s $'\t'
echo
