name: S3 Archive Cleanup

on:
  schedule:
    # Run weekly on Sunday at 05:00 UTC
    - cron: '0 5 * * 0'
  workflow_dispatch:  # Allow manual trigger

permissions:
  contents: read

jobs:
  cleanup:
    runs-on: ubuntu-latest

    steps:
      - name: Install s3cmd
        run: sudo apt-get install -y s3cmd

      - name: Configure s3cmd
        env:
          ACCESS_KEY_ID: ${{ secrets.ACCESS_KEY_ID }}
          SECRET_KEY: ${{ secrets.SECRET_KEY }}
        run: |
          cat > ~/.s3cfg <<EOF
          [default]
          access_key = ${ACCESS_KEY_ID}
          secret_key = ${SECRET_KEY}
          host_base = sfo3.digitaloceanspaces.com
          host_bucket = %(bucket)s.sfo3.digitaloceanspaces.com
          EOF

      - name: Delete old archived game folders
        run: |
          set -euo pipefail

          CUTOFF=$(date -d "1 month ago" +%s)
          DELETED=0
          SKIPPED=0
          NO_DIR_FILE=0

          echo "Cutoff date: $(date -d "@${CUTOFF}" -Iseconds)"
          echo ""

          FOLDERS=$(s3cmd ls s3://eagle0/eagle/archived/ | awk '{print $NF}' | sed 's|s3://eagle0/eagle/archived/||' | sed 's|/||')

          if [ -z "$FOLDERS" ]; then
            echo "No archived folders found."
            exit 0
          fi

          TOTAL=$(echo "$FOLDERS" | wc -l | tr -d ' ')
          CURRENT=0

          for game_id in $FOLDERS; do
            CURRENT=$((CURRENT + 1))

            DIR_INFO=$(s3cmd ls "s3://eagle0/eagle/archived/${game_id}/directory.e0i" 2>/dev/null || true)

            if [ -z "$DIR_INFO" ]; then
              echo "[$CURRENT/$TOTAL] DELETE $game_id (no directory.e0i)"
              s3cmd del --recursive "s3://eagle0/eagle/archived/${game_id}/" > /dev/null 2>&1
              DELETED=$((DELETED + 1))
              NO_DIR_FILE=$((NO_DIR_FILE + 1))
              continue
            fi

            FILE_DATE=$(echo "$DIR_INFO" | awk '{print $1 "T" $2 ":00"}')
            FILE_EPOCH=$(date -d "$FILE_DATE" +%s 2>/dev/null || echo "0")

            if [ "$FILE_EPOCH" -lt "$CUTOFF" ]; then
              echo "[$CURRENT/$TOTAL] DELETE $game_id (directory.e0i from $(echo "$DIR_INFO" | awk '{print $1, $2}'))"
              s3cmd del --recursive "s3://eagle0/eagle/archived/${game_id}/" > /dev/null 2>&1
              DELETED=$((DELETED + 1))
            else
              echo "[$CURRENT/$TOTAL] KEEP $game_id (directory.e0i from $(echo "$DIR_INFO" | awk '{print $1, $2}'))"
              SKIPPED=$((SKIPPED + 1))
            fi
          done

          echo ""
          echo "=== Summary ==="
          echo "Total folders: $TOTAL"
          echo "Deleted: $DELETED"
          echo "Kept (recent): $SKIPPED"
