#!/bin/bash
# Aggregate store stats for selected stores only
# Staging server only - runs 365 day backfill since nightly sync wipes data

STORES="pc80586 pc80711 ou20344 ou20284 ou20487 ou20496 ou20545 ou20927 pc80044 pc80012 pc80064 pc80535 pc80596 pc80955 se60144 se60084 se60027"
DAYS=${1:-0}

cd /home/buyerkiosk/userfrosting

echo "========================================"
echo "Aggregating store stats (selected stores)"
echo "Started: $(date)"
echo "Days to backfill: $DAYS"
echo "========================================"

if [ "$DAYS" -gt 0 ]; then
    # Backfill mode - process each day
    for ((d=DAYS; d>=0; d--)); do
        TARGET_DATE=$(date -d "$d days ago" +%Y-%m-%d)
        echo ""
        echo "--- Processing date: $TARGET_DATE ---"
        for store in $STORES; do
            echo "  $store..."
            php scripts/aggregate-store-stats.php --store=$store --date=$TARGET_DATE
        done
    done
else
    # Normal mode - just today
    for store in $STORES; do
        echo ""
        echo "Processing $store..."
        php scripts/aggregate-store-stats.php --store=$store
    done
fi

echo ""
echo "========================================"
echo "Completed: $(date)"
echo "========================================"
