name: Generate Map Previews for Switzerland25 Tournament

on:
  push:
    paths:
      - '.gitea/workflows/*'
      - 'replays/**'
  workflow_dispatch:

env:
  DANSER_PATH: "/app/danser/danser-cli"
  DANSER_DIR: "/app/danser"
  DANSER_VIDEO_DIR: "/app/danser/videos"
  DANSER_SKINS_DIR: "/app/danser/skins"
  DANSER_MAPS_DIR: "/app/danser/songs"
  SKINS_DIR: "${{ github.workspace }}/skins"
  REPLAY_DIR: "${{ github.workspace }}/replays"
  MAPS_DIR: "${{ github.workspace }}/maps"
  OUTPUT_DIR: "${{ github.workspace }}/outputs"
  TIMESTAMPS_JSON: "${{ github.workspace }}/.gitea/workflows/timestamps.json"
  IMAGE_NAME: osc/skins-image
  REGISTRY_URL: "https://${{ vars.CONTAINER_REGISTRY }}"

jobs:
  generate_everything:
    name: Full CI/CD Pipeline
    runs-on: danser
    container:
      image: ${{ vars.CONTAINER_REGISTRY }}/${{ env.IMAGE_NAME }}:latest
      options: >-
        --gpus all
        --privileged
        --security-opt seccomp=unconfined
        --security-opt apparmor=unconfined
        --cap-add=ALL
        --env NVIDIA_DRIVER_CAPABILITIES=all
        --env NVIDIA_VISIBLE_DEVICES=all
        --user 0:0
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          tags: true
          lfs: true

      - name: Set XDG_RUNTIME_DIR
        shell: bash
        run: |
          echo "Setting XDG_RUNTIME_DIR..."
          mkdir -p /tmp/xdg_runtime_dir
          chmod 0700 /tmp/xdg_runtime_dir
          echo "XDG_RUNTIME_DIR=/tmp/xdg_runtime_dir" >> "$GITHUB_ENV"
          echo "XDG_RUNTIME_DIR set."

      - name: Move necessary files to Danser directory
        shell: bash
        run: |
          mkdir -p "$DANSER_SKINS_DIR"
          mv "$SKINS_DIR"/* "$DANSER_SKINS_DIR"
          mkdir -p "$DANSER_MAPS_DIR"
          find "$MAPS_DIR" -type f -name '*.osz' -exec mv -t "$DANSER_MAPS_DIR" {} +

      - name: Generate Danser videos
        shell: bash
        run: |
          JSON_FILE="$TIMESTAMPS_JSON"
          mapfile -t REPLAYS < <(find "$REPLAY_DIR" -type f -name "*.osr")

          [ ! -f "$JSON_FILE" ] && exit 0

          choose_skin() {
            local name="$1"
            case "$name" in
              *FM*) echo "Aristia(Edit)+trail" ;;
              *HD*) echo "Aristia(Edit)+trail" ;;
              *DT*) echo "boop" ;;
              *EZ*) echo "Jace 6.25" ;;
              *TB*) echo "Aristia(Edit)+trail" ;;
              *HR*) echo "Aristia(Edit)+trail" ;;
              *)    echo "OSCT_2023" ;;
            esac
          }

          for REPLAY in "${REPLAYS[@]}"; do
            [ ! -f "$REPLAY" ] && continue
            REPLAY_NAME=$(basename "$REPLAY" .osr)
            STAGE="${REPLAY_NAME%%_*}"   # take prefix before underscore

            ENTRY=$(jq -c --arg name "$REPLAY_NAME" '
              .[] | map(select(.name == $name)) | .[]' "$JSON_FILE")

            if [ -z "$ENTRY" ]; then
              continue
            fi

            DONE=$(echo "$ENTRY" | jq -r '.done')
            if [ "$DONE" = "true" ]; then
              continue
            fi

            START=$(echo "$ENTRY" | jq -r '.start')
            END=$(echo "$ENTRY" | jq -r '.end')

            OUT_VIDEO_DIR="$OUTPUT_DIR/$STAGE"
            OUT_VIDEO_FILE="$OUT_VIDEO_DIR/$REPLAY_NAME.mp4"

            mkdir -p "$OUT_VIDEO_DIR"

            SKIN=$(choose_skin "$REPLAY_NAME")

            xvfb-run -a "$DANSER_DIR/danser-cli" \
              -replay "$REPLAY" -record -skip -settings="tourneypreview" -skin="$SKIN" -start=$START -end=$END -noupdatecheck \
              -out="$REPLAY_NAME"

            if [ -f "$DANSER_VIDEO_DIR/$REPLAY_NAME.mp4" ]; then
              mv "$DANSER_VIDEO_DIR/$REPLAY_NAME.mp4" "$OUT_VIDEO_FILE"

              tmp=$(mktemp)
              jq --arg name "$REPLAY_NAME" '
                (.. | objects | select(has("name")) | select(.name == $name))
                |= (.done = true)' "$JSON_FILE" > "$tmp" && mv "$tmp" "$JSON_FILE"
            fi
          done

      - name: Configure Git
        shell: bash
        run: |
          git config user.email "arlind@sulej.ch"
          git config user.name "ci-bot"

      - name: Add and Commit changes
        shell: bash
        run: |
          git config advice.addIgnoredFile false

          git add -u outputs/

          git commit -m "[ci skip] push back from pipeline" -q || echo "No changes to commit"

      - name: Push changes and create tag
        shell: bash
        run: git push origin main || echo "No changes to push"
