#!/usr/bin/env bash
# student-launcher.sh — A simple task runner for OpenCode
# CSCD 240 — C and Unix Programming
# TASK FORMAT (tasks.txt): [ ] 1. Description  or  [x] 2. Done task
# USAGE: ./student-launcher.sh          Run next undone task
#        ./student-launcher.sh status   Show progress
#        ./student-launcher.sh add "x"  Add a new task
# Works on macOS, Linux, WSL, and Chromebook.
# This script uses: echo, grep, sed, cut, head, printf, seq — CSCD 240 commands.

set -euo pipefail  # Exit on error, undefined vars, pipe failures
TASKS="tasks.txt"  # The task file lives in the current directory

# --- Check that opencode is installed ---
if ! command -v opencode > /dev/null 2>&1; then  # command -v tests if a program exists
    echo "Error: opencode is not installed."
    echo "Install: curl -fsSL https://opencode.ai/install | bash"
    exit 1
fi

# --- Check that the task file exists ---
if [ ! -f "$TASKS" ]; then  # -f tests if a regular file exists
    echo "Error: $TASKS not found in this directory."
    echo "Create one or download the starter from the course site."
    exit 1
fi

# --- Handle "status" command ---
if [ "${1:-}" = "status" ]; then  # ${1:-} = first arg, or empty string if none
    TOTAL=$(grep -c '^\[' "$TASKS" 2>/dev/null || true)  # Count all task lines
    TOTAL=${TOTAL:-0}  # Default to 0 if grep found nothing
    DONE=$(grep -c '^\[x\]' "$TASKS" 2>/dev/null || true)  # Count completed lines
    DONE=${DONE:-0}  # Default to 0 if grep found nothing
    TODO=$((TOTAL - DONE))  # Arithmetic: subtract done from total
    echo "Progress: $DONE / $TOTAL tasks complete ($TODO remaining)"
    if [ "$TOTAL" -gt 0 ]; then  # Only draw bar if tasks exist
        FILLED=$((DONE * 20 / TOTAL))  # Scale to 20-char wide bar
        EMPTY=$((20 - FILLED))         # Remaining empty slots
        BAR=""; i=0; while [ $i -lt $FILLED ]; do BAR="${BAR}#"; i=$((i+1)); done  # Build # bar
        GAP=""; i=0; while [ $i -lt $EMPTY ];  do GAP="${GAP}-"; i=$((i+1)); done  # Build - gap
        echo "[$BAR$GAP]"
    fi
    exit 0
fi

# --- Handle "add" command ---
if [ "${1:-}" = "add" ]; then
    shift  # Remove "add" from args, leaving the description text
    DESC="$*"  # All remaining arguments become the task description
    if [ -z "$DESC" ]; then  # -z tests if string is empty
        echo "Usage: ./student-launcher.sh add \"description of task\""
        exit 1
    fi
    NEXT=$(( $(grep -c '^\[' "$TASKS" 2>/dev/null || echo 0) + 1 ))  # Next task number
    echo "[ ] $NEXT. $DESC" >> "$TASKS"  # Append new undone task to file
    echo "Added task $NEXT: $DESC"
    exit 0
fi

# --- Default: run the next undone task ---
TASK_LINE=$(grep -n '^\[ \]' "$TASKS" | head -1)  # First line starting with [ ] (undone)
if [ -z "$TASK_LINE" ]; then  # No undone tasks found
    echo "All tasks complete. Run: ./student-launcher.sh status"
    exit 0
fi
LINE_NUM=$(echo "$TASK_LINE" | cut -d: -f1)  # Extract line number before the colon
TASK_DESC=$(echo "$TASK_LINE" | sed 's/^[0-9]*:\[ \] [0-9]*\. //')  # Extract description

echo "Task: $TASK_DESC"
echo "Sending to OpenCode..."
echo ""
opencode run "$TASK_DESC"  # Run the task — output goes to your terminal

# Mark done: sed -i.bak works on both macOS and Linux/WSL
sed -i.bak "${LINE_NUM}s/^\[ \]/[x]/" "$TASKS"  # Replace [ ] with [x] on that line
rm -f "$TASKS.bak"  # Clean up backup file
echo ""
echo "Done. Run ./student-launcher.sh for the next task."
