#!/usr/bin/env bash
# Read local pmo-roadmap work logs.

set -eu

usage() {
  cat <<EOF
Usage: work-log-read [options]

Options:
  --date YYYY-MM-DD        Day to read (default: today)
  --log-dir DIR            Work-log root (default: \$PMO_WORK_LOG_DIR or ~/.work/log)
  --identity ID            Read {ID}-work-summary.log for the date
  --log-file FILE          Read one specific log file
  --max-lines N            Print at most N lines with an explicit
                           truncation marker (default 0 = full output)
  --list                   List matching logs instead of printing content
  -h, --help               Show this help

Default behavior:
  - prints the only work-summary log for the day, if exactly one exists
  - lists logs if multiple exist
EOF
}

die() {
  echo "work-log-read: $1" >&2
  exit 1
}

DATE="$(date +%F)"
# PMO_WORK_LOG_DIR precedence: pre-commit.config > environment > default;
# the --log-dir flag beats all three.
LOG_DIR="${PMO_WORK_LOG_DIR:-${HOME:-}/.work/log}"
REPO_TOPLEVEL="$(git rev-parse --show-toplevel 2>/dev/null || true)"
if [ -n "$REPO_TOPLEVEL" ] && [ -f "$REPO_TOPLEVEL/.githooks/pre-commit.config" ]; then
  # shellcheck disable=SC1091
  . "$REPO_TOPLEVEL/.githooks/pre-commit.config"
  LOG_DIR="${PMO_WORK_LOG_DIR:-$LOG_DIR}"
fi
IDENTITY=""
LOG_FILE=""
LIST_ONLY=0
MAX_LINES=0

while [ $# -gt 0 ]; do
  case "$1" in
    -h|--help) usage; exit 0 ;;
    --date) DATE="$2"; shift 2 ;;
    --log-dir) LOG_DIR="$2"; shift 2 ;;
    --identity) IDENTITY="$2"; shift 2 ;;
    --log-file) LOG_FILE="$2"; shift 2 ;;
    --max-lines) MAX_LINES="$2"; shift 2 ;;
    --list) LIST_ONLY=1; shift ;;
    --) shift; break ;;
    -*) die "unknown option: $1" ;;
    *) die "unexpected argument: $1" ;;
  esac
done

case "$MAX_LINES" in
  *[!0-9]*) die "--max-lines expects a non-negative integer" ;;
esac

print_file() {
  [ -f "$1" ] || die "log file does not exist: $1"
  if [ "$MAX_LINES" -gt 0 ]; then
    total=$(wc -l < "$1" | tr -d ' ')
    sed -n "1,${MAX_LINES}p" "$1"
    if [ "$total" -gt "$MAX_LINES" ]; then
      echo "[PMO_WORK_LOG_READ_TRUNCATED: showing $MAX_LINES of $total lines; use --max-lines 0 for full output]"
    fi
  else
    cat "$1"
  fi
}

if [ -n "$LOG_FILE" ]; then
  if [ "$LIST_ONLY" -eq 1 ]; then
    [ -f "$LOG_FILE" ] || die "log file does not exist: $LOG_FILE"
    printf '%s\n' "$LOG_FILE"
  else
    print_file "$LOG_FILE"
  fi
  exit 0
fi

day_dir="$LOG_DIR/$DATE"
[ -d "$day_dir" ] || die "no log directory for date: $day_dir"

if [ -n "$IDENTITY" ]; then
  target="$day_dir/${IDENTITY}-work-summary.log"
  if [ "$LIST_ONLY" -eq 1 ]; then
    [ -f "$target" ] || die "log file does not exist: $target"
    printf '%s\n' "$target"
  else
    print_file "$target"
  fi
  exit 0
fi

matches=$(find "$day_dir" -maxdepth 1 -type f -name '*-work-summary.log' 2>/dev/null | sort)
count=$(printf '%s\n' "$matches" | sed '/^[[:space:]]*$/d' | wc -l | tr -d ' ')

if [ "$count" = "0" ]; then
  die "no work-summary logs found in $day_dir"
fi

if [ "$LIST_ONLY" -eq 1 ] || [ "$count" != "1" ]; then
  printf '%s\n' "$matches"
else
  print_file "$(printf '%s\n' "$matches" | sed -n '1p')"
fi

exit 0
