#!/usr/bin/env bash
set -euo pipefail

TOKEN="$(grep '^PHENOCRYPTO_ADMIN_TOKEN=' /etc/phenocrypto.env | cut -d= -f2-)"
USERNAME="$(grep '^PHENOCRYPTO_USERNAME=' /etc/phenocrypto.env | cut -d= -f2-)"
PASSWORD="$(grep '^PHENOCRYPTO_PASSWORD=' /etc/phenocrypto.env | cut -d= -f2-)"
COOKIE_JAR="/tmp/phenocrypto-cookies.txt"

echo "service=$(systemctl is-active phenocrypto.service)"

curl -kfsSL "https://muxiyeto.com/phenocrypto/" -o /tmp/phenocrypto-public-redirect.html
curl -kfsS "https://muxiyeto.com/phenocrypto/login" -o /tmp/phenocrypto-public-login.html
curl -kfsS \
  -c "$COOKIE_JAR" \
  -H "Content-Type: application/json" \
  -d "{\"username\":\"$USERNAME\",\"password\":\"$PASSWORD\"}" \
  "https://muxiyeto.com/phenocrypto/api/login" \
  -o /tmp/phenocrypto-login.json
curl -kfsS \
  -X POST \
  -H "Content-Type: application/json" \
  -b "$COOKIE_JAR" \
  -d '{}' \
  "https://muxiyeto.com/phenocrypto/api/service/start" \
  -o /tmp/phenocrypto-service-start.json
curl -fsS \
  -H "X-Admin-Token: $TOKEN" \
  "http://127.0.0.1:3105/phenocrypto/api/status" \
  -o /tmp/phenocrypto-local-status.json
curl -kfsS \
  -b "$COOKIE_JAR" \
  "https://muxiyeto.com/phenocrypto/api/status" \
  -o /tmp/phenocrypto-public-status.json
curl -kfsS \
  -b "$COOKIE_JAR" \
  "https://muxiyeto.com/phenocrypto/api/credentials" \
  -o /tmp/phenocrypto-public-credentials.json
curl -kfsS \
  -b "$COOKIE_JAR" \
  "https://muxiyeto.com/phenocrypto/dashboard" \
  -o /tmp/phenocrypto-public-dashboard.html
curl -kfsS \
  -b "$COOKIE_JAR" \
  "https://muxiyeto.com/phenocrypto/trading" \
  -o /tmp/phenocrypto-public-trading.html
curl -kfsS \
  -b "$COOKIE_JAR" \
  "https://muxiyeto.com/phenocrypto/settings" \
  -o /tmp/phenocrypto-public-settings.html
curl -kfsS \
  -b "$COOKIE_JAR" \
  "https://muxiyeto.com/phenocrypto/static/app.js?v=20260610-bot-status" \
  -o /tmp/phenocrypto-public-app.js
curl -kfsS \
  -b "$COOKIE_JAR" \
  "https://muxiyeto.com/phenocrypto/api/chart/XBTUSD" \
  -o /tmp/phenocrypto-public-chart.json
curl -kfsS \
  -X POST \
  -H "Content-Type: application/json" \
  -b "$COOKIE_JAR" \
  -d '{}' \
  "https://muxiyeto.com/phenocrypto/api/run" \
  -o /tmp/phenocrypto-public-run.json
curl -kfsS \
  -X POST \
  -H "Content-Type: application/json" \
  -b "$COOKIE_JAR" \
  -d '{}' \
  "https://muxiyeto.com/phenocrypto/api/service/stop" \
  -o /tmp/phenocrypto-service-stop.json
curl -kfsS \
  -b "$COOKIE_JAR" \
  "https://muxiyeto.com/phenocrypto/api/status" \
  -o /tmp/phenocrypto-public-status-stopped.json
curl -kfsS \
  -X POST \
  -H "Content-Type: application/json" \
  -b "$COOKIE_JAR" \
  -d '{}' \
  "https://muxiyeto.com/phenocrypto/api/service/start" \
  -o /tmp/phenocrypto-service-restart.json

python3 - <<'PY'
import json
from pathlib import Path

for label, path in [
    ("local", "/tmp/phenocrypto-local-status.json"),
    ("public", "/tmp/phenocrypto-public-status.json"),
]:
    data = json.loads(Path(path).read_text())
    print(f"{label}=ok:{data['ok']} dry_run:{data['dry_run']} pairs:{len(data['pairs'])}")
    print(f"{label}_bot_status=service:{data['bot_status']['service']} loop:{data['bot_status']['loop_running']} open:{data['bot_status']['open_position_count']}")

login_html = Path("/tmp/phenocrypto-public-login.html").read_text()
dashboard_html = Path("/tmp/phenocrypto-public-dashboard.html").read_text()
trading_html = Path("/tmp/phenocrypto-public-trading.html").read_text()
settings_html = Path("/tmp/phenocrypto-public-settings.html").read_text()
app_js = Path("/tmp/phenocrypto-public-app.js").read_text()
login = json.loads(Path("/tmp/phenocrypto-login.json").read_text())
print(f"login_page_has_form={'loginForm' in login_html}")
print(f"dashboard_has_title={'Phenocrypto' in dashboard_html}")
print(f"login=ok:{login['ok']}")

run = json.loads(Path("/tmp/phenocrypto-public-run.json").read_text())
service_start = json.loads(Path("/tmp/phenocrypto-service-start.json").read_text())
service_stop = json.loads(Path("/tmp/phenocrypto-service-stop.json").read_text())
service_restart = json.loads(Path("/tmp/phenocrypto-service-restart.json").read_text())
stopped_status = json.loads(Path("/tmp/phenocrypto-public-status-stopped.json").read_text())
credentials = json.loads(Path("/tmp/phenocrypto-public-credentials.json").read_text())
chart = json.loads(Path("/tmp/phenocrypto-public-chart.json").read_text())
first = run["results"][0]
print(f"run=ok:{run['ok']} results:{len(run['results'])} first:{first['symbol']}:{first['signal']['side']}")
print(f"credentials=ok:{credentials['ok']} has_key:{credentials['credentials']['has_api_key']}")
print(f"dashboard_has_symbols={'symbolStrip' in dashboard_html and 'apiForm' not in dashboard_html and 'tradeChart' not in dashboard_html}")
print(f"dashboard_has_bot_status={'Bot Status' in dashboard_html and 'lastEntryStatus' in dashboard_html and 'lastCloseStatus' in dashboard_html}")
print(f"dashboard_has_service_buttons={'startServiceBtn' in dashboard_html and 'stopServiceBtn' in dashboard_html}")
print(f"service_start=ok:{service_start['ok']} state:{service_start['service']['state']}")
print(f"service_stop=ok:{service_stop['ok']} state:{service_stop['service']['state']} status_after_stop:{stopped_status['bot_status']['service']}")
print(f"service_restart=ok:{service_restart['ok']} state:{service_restart['service']['state']}")
print(f"trading_has_chart={'tradeChart' in trading_html and 'apiForm' not in trading_html and 'drawChart' in app_js}")
print(f"settings_has_api_form={'apiForm' in settings_html and 'tradeChart' not in settings_html}")
print(f"windows_are_separate={'dashboard' in dashboard_html and 'Trading Window' in trading_html and 'Kraken API Keys' in settings_html}")
print(f"chart=ok:{chart['ok']} symbol:{chart['chart']['symbol']} candles:{len(chart['chart']['candles'])} markers:{len(chart['chart']['markers'])}")
PY
