#!/bin/zsh
set -e
PROJECT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
cd "$PROJECT_DIR"
PROMPT="$(/usr/bin/osascript <<'APPLESCRIPT'
try
  set initialText to the clipboard as text
on error
  set initialText to ""
end try
set dialogResult to display dialog "Describe the change you want Codex to make in LaLigaBrains." with title "LaLigaBrains Website Agent" default answer initialText buttons {"Cancel", "Start new chat"} default button "Start new chat" cancel button "Cancel"
return text returned of dialogResult
APPLESCRIPT
)"
if [[ -z "${PROMPT//[[:space:]]/}" ]]; then
  exit 0
fi
CODEX="/Applications/ChatGPT.app/Contents/Resources/codex-cli/CodexCLI.app/Contents/MacOS/codex"
if [[ ! -x "$CODEX" ]]; then
  CODEX="$(command -v codex || true)"
fi
if [[ -z "$CODEX" ]]; then
  print -u2 "Codex CLI was not found. Open ChatGPT once and try again."
  exit 1
fi
PROMPT+=$'\n\nAfter finishing, create website-upload.zip in the project root. It must be a small static website with index.html at the ZIP root, under 5 MB compressed, under 12 MB expanded, and no more than 50 files. Include image files as separate ZIP files when the site uses them, and reference them by relative paths; the Kingdom uploader stores each image separately in Firebase Realtime Database and compresses it if needed to keep the encoded record below 1 MB. The saved website ZIP contains code only. Do not include videos, inline data:image content, or publish/upload anything from Codex. Leave the ZIP ready for Reino → Versiones de la web.'
exec "$CODEX" --cd "$PROJECT_DIR" --sandbox workspace-write --ask-for-approval on-request "$PROMPT"
