apiVersion: argoproj.io/v1alpha1
kind: AnalysisTemplate
metadata:
  name: market-hours-check
  namespace: stonks-oracle
spec:
  metrics:
    - name: outside-market-hours
      provider:
        job:
          spec:
            ttlSecondsAfterFinished: 3600
            backoffLimit: 0
            template:
              spec:
                containers:
                  - name: check
                    image: alpine:3.19
                    command: [sh, -c]
                    args:
                      - |
                        apk add --no-cache tzdata
                        export TZ=America/New_York
                        DOW=$(date +%u)  # 1=Mon, 7=Sun
                        HOUR=$(date +%H)
                        MIN=$(date +%M)
                        TIME_MIN=$((HOUR * 60 + MIN))
                        MARKET_OPEN=570   # 09:30
                        MARKET_CLOSE=960  # 16:00
                        if [ "$DOW" -ge 6 ]; then
                          echo "Weekend — promotions allowed"
                          exit 0
                        fi
                        if [ "$TIME_MIN" -lt "$MARKET_OPEN" ] || [ "$TIME_MIN" -ge "$MARKET_CLOSE" ]; then
                          echo "Outside market hours — promotions allowed"
                          exit 0
                        fi
                        echo "Market hours active ($(date)) — promotion blocked"
                        exit 1
                restartPolicy: Never
