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

HOST=""
REMOTE_ROOT="/opt/lingniu-go-native"
DAYS_BACK=1
WINDOW_DAYS=3
PROTOCOLS="GB32960,JT808,YUTONG_MQTT"
ON_CALENDAR="*-*-* 01:30:00"
DRY_RUN="false"

usage() {
  cat <<'USAGE'
Usage:
  scripts/install-stats-backfill-timer.sh --host root@115.29.187.205 [options]

Options:
  --host HOST          SSH target for ECS installation.
  --remote-root DIR   Remote install root. Defaults to /opt/lingniu-go-native.
  --days-back N       Target end date relative to local day. Defaults to 1, meaning yesterday.
  --window-days N     Number of days to recalculate ending at days-back. Defaults to 3.
  --protocols LIST    Comma-separated protocols. Defaults to GB32960,JT808,YUTONG_MQTT.
  --on-calendar SPEC  systemd OnCalendar value. Defaults to "*-*-* 01:30:00".
  --dry-run           Install timer in dry-run mode.
  -h, --help          Show this help.
USAGE
}

while [[ $# -gt 0 ]]; do
  case "$1" in
    --host)
      HOST="${2:-}"
      shift 2
      ;;
    --remote-root)
      REMOTE_ROOT="${2:-}"
      shift 2
      ;;
    --days-back)
      DAYS_BACK="${2:-}"
      shift 2
      ;;
    --window-days)
      WINDOW_DAYS="${2:-}"
      shift 2
      ;;
    --protocols)
      PROTOCOLS="${2:-}"
      shift 2
      ;;
    --on-calendar)
      ON_CALENDAR="${2:-}"
      shift 2
      ;;
    --dry-run)
      DRY_RUN="true"
      shift
      ;;
    -h|--help)
      usage
      exit 0
      ;;
    *)
      echo "unknown argument: $1" >&2
      usage >&2
      exit 2
      ;;
  esac
done

if [[ -z "$HOST" ]]; then
  echo "--host is required" >&2
  usage >&2
  exit 2
fi

case "$DAYS_BACK" in
  ''|*[!0-9]*)
    echo "--days-back must be a non-negative integer" >&2
    exit 2
    ;;
esac
case "$WINDOW_DAYS" in
  ''|*[!0-9]*|0)
    echo "--window-days must be a positive integer" >&2
    exit 2
    ;;
esac

remote_cmd=""
printf -v remote_cmd \
  "REMOTE_ROOT=%q DAYS_BACK=%q WINDOW_DAYS=%q PROTOCOLS=%q ON_CALENDAR=%q DRY_RUN=%q bash -s" \
  "$REMOTE_ROOT" "$DAYS_BACK" "$WINDOW_DAYS" "$PROTOCOLS" "$ON_CALENDAR" "$DRY_RUN"
ssh "$HOST" "$remote_cmd" <<'REMOTE'
set -euo pipefail

install -d /etc/systemd/system

cat >/etc/systemd/system/lingniu-go-stats-backfill.service <<SERVICE
[Unit]
Description=Lingniu Go Stats Backfill
Documentation=file:${REMOTE_ROOT}/current/docs/ops/vehicle-ingest-runbook.md
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
WorkingDirectory=${REMOTE_ROOT}/current
Environment=BACKFILL_METHOD=last_diff
Environment=BACKFILL_DRY_RUN=${DRY_RUN}
Environment=BACKFILL_DAYS_BACK=${DAYS_BACK}
Environment=BACKFILL_WINDOW_DAYS=${WINDOW_DAYS}
Environment=BACKFILL_PROTOCOLS=${PROTOCOLS}
ExecStart=${REMOTE_ROOT}/current/stats-backfill
TimeoutStartSec=45min
StandardOutput=journal
StandardError=journal
SERVICE

cat >/etc/systemd/system/lingniu-go-stats-backfill.timer <<TIMER
[Unit]
Description=Run Lingniu Go Stats Backfill daily

[Timer]
OnCalendar=${ON_CALENDAR}
AccuracySec=1min
Persistent=false
Unit=lingniu-go-stats-backfill.service

[Install]
WantedBy=timers.target
TIMER

systemctl daemon-reload
systemd-analyze verify /etc/systemd/system/lingniu-go-stats-backfill.service /etc/systemd/system/lingniu-go-stats-backfill.timer
systemctl reset-failed lingniu-go-stats-backfill.service 2>/dev/null || true
systemctl enable --now lingniu-go-stats-backfill.timer
systemctl list-timers lingniu-go-stats-backfill.timer --no-pager
REMOTE
