#!/usr/bin/env bash
# Exercise 2: convert curl -w cumulative timings into per-stage durations (medium)
#
# This exercise does NOT require live network access — it analyzes canned
# sample curl -w output (embedded below), matching theory section 5's point
# that curl -w fields are CUMULATIVE from request start, not per-stage.
#
# Sample input format (one "key: value" pair per line, value in seconds):
#   dns: 0.021
#   connect: 0.036
#   tls: 0.061
#   ttfb: 0.245
#   total: 0.287
#
# STEP 1: implement `stage_duration` — given the sample block (on stdin) and
#         two field names (earlier, later), print the per-stage duration in
#         milliseconds: (later_value - earlier_value) * 1000, formatted with
#         1 decimal place (e.g. "184.0").
#
# STEP 2: implement `slowest_stage` — given the sample block (on stdin),
#         compute all four per-stage durations (dns, tcp-connect, tls,
#         ttfb — see the field pairs below) and print the name of the
#         slowest one. Stage definitions:
#           dns          = dns - 0            (dns lookup starts at t=0)
#           tcp-connect  = connect - dns
#           tls          = tls - connect
#           ttfb         = ttfb - tls
#         (ignore "total" — it's not one of the four stages being compared)

set -euo pipefail

stage_duration() {
  local input="$1" earlier_field="$2" later_field="$3"
  # TODO: parse `earlier_field: X` and `later_field: Y` out of $input,
  # compute (Y - X) * 1000, print with 1 decimal place.
  # Hint: grep "^${field}:" <<< "$input" | awk '{print $2}'
  #       use `bc -l` or `awk` for the floating-point arithmetic.
  echo ""
}

slowest_stage() {
  local input="$1"
  # TODO: compute all four stage durations via stage_duration and print the
  # name of the largest one (one of: dns, tcp-connect, tls, ttfb).
  echo ""
}

# --- self-check: do not modify below this line ---
SAMPLE_TIMING='dns: 0.021
connect: 0.036
tls: 0.061
ttfb: 0.245
total: 0.287'

pass=true

got1=$(stage_duration "$SAMPLE_TIMING" "connect" "tls")
# tls (0.061) - connect (0.036) = 0.025s = 25.0ms
if [[ "$got1" == "25.0" ]]; then
  echo "PASS: stage_duration computes the TLS handshake duration correctly"
else
  echo "FAIL: stage_duration computes the TLS handshake duration correctly (got '$got1', expected '25.0')"
  pass=false
fi

got2=$(stage_duration "$SAMPLE_TIMING" "tls" "ttfb")
# ttfb (0.245) - tls (0.061) = 0.184s = 184.0ms
if [[ "$got2" == "184.0" ]]; then
  echo "PASS: stage_duration computes the TTFB (server processing) duration correctly"
else
  echo "FAIL: stage_duration computes the TTFB duration correctly (got '$got2', expected '184.0')"
  pass=false
fi

got3=$(slowest_stage "$SAMPLE_TIMING")
if [[ "$got3" == "ttfb" ]]; then
  echo "PASS: slowest_stage identifies ttfb as the bottleneck (184ms dwarfs the other stages)"
else
  echo "FAIL: slowest_stage identifies ttfb as the bottleneck (got '$got3', expected 'ttfb')"
  pass=false
fi

if $pass; then
  echo "PASS"
else
  echo "FAIL"
fi
