#!/bin/bash
# Daily reward calculation cronjob script
# This script calculates rewards for yesterday's date
# Should run daily at 00:00 UTC+8 (Asia/Kuala_Lumpur)
# Django TIME_ZONE is configured to Asia/Kuala_Lumpur

# Set timezone to UTC+8 (matches Django TIME_ZONE setting)
export TZ=Asia/Kuala_Lumpur

# Get the project directory (assuming script is in scripts/ directory)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
BACKEND_DIR="$PROJECT_DIR/backend"

# Calculate yesterday's date in YYYY-MM-DD format (using Django's timezone)
YESTERDAY=$(TZ=Asia/Kuala_Lumpur date -d "yesterday" +%Y-%m-%d)

# Change to backend directory
cd "$BACKEND_DIR" || exit 1

# Activate virtual environment if it exists (adjust path as needed)
# source venv/bin/activate  # Uncomment if using virtualenv
# source .venv/bin/activate  # Uncomment if using .venv

# Run the management command
python manage.py calculate_rewards --date "$YESTERDAY"

# Log the execution (optional)
echo "$(date '+%Y-%m-%d %H:%M:%S %Z'): Calculated rewards for $YESTERDAY" >> /var/log/telegram-earn-rewards.log 2>&1
