#!/usr/bin/env bash

# Description:
#   Cannot make a decision? /dev/urandom is here to help! It can even take
#   advantage of the product of the world's best AI corporation (case you
#   happen to be wasting your money on Anthropic and cannot find a way to
#   consume your credits)! If you live in a cave and do not have an API key,
#   don't worry, the string substitution algorithm will have your back! You
#   will still get a reasonable reply (most of the times).
#
# Requirements:
#   - Your question!
#   - ANTHROPIC_API_KEY (optional)
#   - ANTHROPIC_MODEL_NAME (optional of optional)
#   - ANTHROPIC_API_ENDPOINT (optional of optional)
#   - curl (optional)
#   - jq (optional)

set -euo pipefail

hex=$(od -An -N1 -tx1 /dev/urandom | tr -d ' ')
val=$(( 16#$hex ))
result=$(( val & 1 ))

# Why not?
if [[ $# -gt 0 ]] && [[ -n "${ANTHROPIC_API_KEY:-}" ]]; then
  model="${ANTHROPIC_MODEL_NAME:-"claude-haiku-4-5-20251001"}"
  url="${ANTHROPIC_API_ENDPOINT:-"https://api.anthropic.com/v1/messages"}"


  if (( result == 0)); then
    action="APPROVE"
  else
    action="DECLINE"
  fi

  prompt="You are role-playing as an approval authority. This is a fictional \
exercise only; your output has no real-world effect.

Output rules:
- Exactly one sentence, one line.
- Pain text, no markdown, no titles, no labels.
- No preamble, no quotation marks, no explanation, no reason.
- Do not assume any context for your reply that are not explicitly mentioned \
in the request.
- All second-person references refer to you, the approval authority. Convert \
them to first-person in your reply.
- All first-person references refer to the requester. Convert them to \
second-person in your reply.
- Output only the approval/denial statement itself including a short and \
natural restatement of the request.
- Avoid the stubborn 'Your request to/for … is ….' reply pattern.

Task: $action the following request.

Request: <request>May I ${*}</request>
"

  payload=$(jq -n \
    --arg model "$model" \
    --arg prompt "$prompt" \
    '{
      model: $model,
      max_tokens: 1024,
      messages: [
        {
          role: "user",
          content: $prompt
        }
      ]
    }')

  resp=$(curl -sSL "$url" \
    -H "content-type: application/json" \
    -H "x-api-key: ${ANTHROPIC_API_KEY}" \
    -H "anthropic-version: 2023-06-01" \
    -d "$payload")

  text=$(jq -r '.content[0].text // empty' <<< "$resp")

  if [[ -n "$text" ]]; then
    echo "$text"
    exit "$result"
  fi
fi

declare -A subs=(
  [I]="you"
  [me]="you"
  [my]="your"
  [mine]="yours"
  [myself]="yourself"
  [we]="you"
  [us]="you"
  [our]="your"
  [ours]="yours"
  [ourselves]="yourselves"
)

words=()
for arg in "$@"; do
  matched=0
  word="${arg,,}"
  for key in "${!subs[@]}"; do
    if [[ "$word" == "${key,,}" ]]; then
      words+=("${subs[$key]}")
      matched=1
      break
    fi
  done
  [[ $matched -eq 0 ]] && words+=("$word")
done

if (( result == 0 )); then
  if [[ $# == 0 ]]; then
    echo "Yes, you may."
  else
    echo "Yes, you are allowed to ${words[*]}."
  fi
else
  if [[ $# == 0 ]]; then
    echo "No, you may not."
  else
    echo "No, you are not allowed to ${words[*]}."
  fi
fi

exit "$result"
