#!/bin/sh
input=$(cat)
model=$(echo "$input" | sed -n 's/.*"display_name" *: *"\([^"]*\)".*/\1/p')
ctx=$(echo "$input" | grep -o '"used_percentage":[0-9.]*' | head -1 | grep -o '[0-9.]*')
session=$(echo "$input" | grep -o '"five_hour":{[^}]*' | grep -o '"used_percentage":[0-9.]*' | grep -o '[0-9.]*')

# Adom brand colors (truecolor)
C_TEAL=$(printf '\033[38;2;0;184;177m')
C_BLUE=$(printf '\033[38;2;100;171;255m')
C_PURPLE=$(printf '\033[38;2;140;107;247m')
C_RED=$(printf '\033[38;2;255;100;100m')
C_YELLOW=$(printf '\033[38;2;255;193;7m')
C_DIM=$(printf '\033[38;2;120;120;120m')
C_RESET=$(printf '\033[0m')

if [ -n "$model" ]; then
  printf "%s%s%s" "$C_PURPLE" "$model" "$C_RESET"
fi

if [ -n "$ctx" ]; then
  ctx_int=$(printf '%.0f' "$ctx")
  filled=$(( ctx_int / 5 ))
  empty=$(( 20 - filled ))

  if [ "$ctx_int" -ge 80 ]; then
    bar_color="$C_RED"
  elif [ "$ctx_int" -ge 50 ]; then
    bar_color="$C_YELLOW"
  else
    bar_color="$C_TEAL"
  fi

  bar=""
  i=0
  while [ $i -lt $filled ]; do
    bar="${bar}━"
    i=$(( i + 1 ))
  done
  i=0
  while [ $i -lt $empty ]; do
    bar="${bar}╌"
    i=$(( i + 1 ))
  done

  printf " %s[%s %s%%]%s" "$bar_color" "$bar" "$ctx_int" "$C_RESET"
fi

if [ -n "$session" ]; then
  session_int=$(printf '%.0f' "$session")
  if [ "$session_int" -ge 80 ]; then
    s_color="$C_RED"
  elif [ "$session_int" -ge 50 ]; then
    s_color="$C_YELLOW"
  else
    s_color="$C_BLUE"
  fi
  printf " %s%s %s%%%s" "$C_DIM" "5h:" "$s_color$session_int" "$C_RESET"
fi
