#!/bin/bash
set -e

DATE=$(date -u +%Y-%m-%d)
echo "[$(date -u +%Y-%m-%dT%H:%M:%SZ)] Running snapshot for $DATE"

# Write ODDS_API_KEY to env file so betlab CLI can find it
mkdir -p ~/.betlab
echo "ODDS_API_KEY=${ODDS_API_KEY}" > ~/.betlab/.env

# Configure git remote with token auth (no SSH key needed)
cd /app
git remote set-url origin "https://oauth2:${GITLAB_TOKEN}@gitlab.com/hvaleanu/betlab.git"

# Pull latest code
git pull origin main --quiet

# Reinstall in case pyproject.toml changed
pip install -e . --quiet

# Fetch snapshot and commit+push
betlab snapshot --commit

echo "[$(date -u +%Y-%m-%dT%H:%M:%SZ)] Done. Snapshot committed."
