name: Deploy

on:
  push:
    branches:
      - main
      - version/*

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout sources
        uses: actions/checkout@v6
      - name: Setup Java 8
        uses: actions/setup-java@v5
        with:
          distribution: 'temurin'
          java-version: 8
      - name: Setup Java 11
        uses: actions/setup-java@v5
        with:
          distribution: 'temurin'
          java-version: 11
      - name: Setup Java 17
        uses: actions/setup-java@v5
        with:
          distribution: 'temurin'
          java-version: 17
      - name: Setup Java
        uses: actions/setup-java@v5
        with:
          distribution: 'temurin'
          java-version: 21
      - name: Setup Gradle
        uses: gradle/actions/setup-gradle@v6
      - name: Setup Maven Repository
        env:
          SW_MAVEN_CREDENTIALS: ${{ secrets.SW_MAVEN_CREDENTIALS }}
        run: |
          echo "$SW_MAVEN_CREDENTIALS" > steamwar.properties
      - name: Build with Gradle
        run: ./gradlew clean build --no-daemon --no-build-cache --refresh-dependencies 

      - name: Stage deploy artifacts
        shell: bash
        run: |
          set -euo pipefail

          rm -rf deploy
          mkdir -p deploy

          cp "BauSystem/build/libs/BauSystem-all.jar" "deploy/BauSystem.jar"
          cp "FightSystem/build/libs/FightSystem-all.jar" "deploy/FightSystem.jar"
          cp "KotlinCore/build/libs/KotlinCore-all.jar" "deploy/KotlinCore.jar"
          cp "TNTLeague/build/libs/TNTLeague.jar" "deploy/TNTLeague.jar"
          cp "LobbySystem/build/libs/LobbySystem.jar" "deploy/LobbySystem.jar"
          cp "MissileWars/build/libs/MissileWars.jar" "deploy/MissileWars.jar"
          cp "Realtime/build/libs/Realtime.jar" "deploy/RealTime.jar"
          cp "SchematicSystem/build/libs/SchematicSystem-all.jar" "deploy/SchematicSystem.jar"
          cp "SpigotCore/build/libs/SpigotCore-all.jar" "deploy/SpigotCore.jar"
          cp "Teamserver/build/libs/Teamserver.jar" "deploy/Builder.jar"
          cp "TowerRun/build/libs/TowerRun.jar" "deploy/TowerRun.jar"
          cp "VelocityCore/Persistent/build/libs/Persistent.jar" "deploy/PersistentVelocityCore.jar"
          cp "VelocityCore/Dependencies/build/libs/Dependencies-all.jar" "deploy/DependenciesVelocityCore.jar"
          cp "VelocityCore/build/libs/VelocityCore-all.jar" "deploy/VelocityCore.jar"
          cp "WebsiteBackend/build/libs/WebsiteBackend-all.jar" "deploy/website-api.jar"
          cp "CLI/build/distributions/sw.zip" "deploy/sw.zip"

      - name: Upload deploy artifacts
        uses: actions/upload-artifact@v3
        with:
          name: steamwar-jars
          path: deploy/

  deploy:
    name: Deploy
    runs-on: ubuntu-latest
    needs: build
    steps:
      - name: Download deploy artifacts
        uses: actions/download-artifact@v3
        with:
          name: steamwar-jars
          path: deploy

      - name: Resolve deploy target
        id: target
        shell: bash
        run: |
          set -euo pipefail

          if [[ "${GITHUB_REF_NAME}" == "main" ]]; then
            echo "path=/jars/current" >> "$GITHUB_OUTPUT"
          elif [[ "${GITHUB_REF_NAME}" == version/* ]]; then
            version="${GITHUB_REF_NAME#version/}"
            if [[ ! "$version" =~ ^[A-Za-z0-9._-]+$ ]]; then
              echo "Unsupported version branch name: ${GITHUB_REF_NAME}" >&2
              exit 1
            fi
            echo "path=/jars/${version}" >> "$GITHUB_OUTPUT"
          else
            echo "Unsupported deployment branch: ${GITHUB_REF_NAME}" >&2
            exit 1
          fi

      - name: Upload jars with scp
        shell: bash
        env:
          DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
          DEPLOY_USER: ${{ secrets.DEPLOY_USER }}
          DEPLOY_PORT: ${{ secrets.DEPLOY_PORT }}
          DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
          DEPLOY_PATH: ${{ steps.target.outputs.path }}
        run: |
          set -euo pipefail

          : "${DEPLOY_HOST:?Missing DEPLOY_HOST secret}"
          : "${DEPLOY_USER:?Missing DEPLOY_USER secret}"
          : "${DEPLOY_SSH_KEY:?Missing DEPLOY_SSH_KEY secret}"

          port="${DEPLOY_PORT:-22}"

          mkdir -p ~/.ssh
          chmod 700 ~/.ssh
          echo "$DEPLOY_SSH_KEY" > ~/.ssh/deploy_key
          chmod 600 ~/.ssh/deploy_key
          ssh-keyscan -p "$port" "$DEPLOY_HOST" >> ~/.ssh/known_hosts

          ssh -i ~/.ssh/deploy_key -p "$port" "${DEPLOY_USER}@${DEPLOY_HOST}" "mkdir -p '$DEPLOY_PATH'"
          ssh -i ~/.ssh/deploy_key -p "$port" "${DEPLOY_USER}@${DEPLOY_HOST}" "mkdir -p '$DEPLOY_PATH/upload'"
          scp -i ~/.ssh/deploy_key -P "$port" deploy/* "${DEPLOY_USER}@${DEPLOY_HOST}:$DEPLOY_PATH/upload"
          ssh -i ~/.ssh/deploy_key -p "$port" "${DEPLOY_USER}@${DEPLOY_HOST}" "rm -f '$DEPLOY_PATH'/*.jar '$DEPLOY_PATH'/*.zip"
          ssh -i ~/.ssh/deploy_key -p "$port" "${DEPLOY_USER}@${DEPLOY_HOST}" "mv '$DEPLOY_PATH'/upload/* '$DEPLOY_PATH'"
          ssh -i ~/.ssh/deploy_key -p "$port" "${DEPLOY_USER}@${DEPLOY_HOST}" "rm -r '$DEPLOY_PATH/upload'"
          ssh -i ~/.ssh/deploy_key -p "$port" "${DEPLOY_USER}@${DEPLOY_HOST}" "chmod o-w '$DEPLOY_PATH'/*"
      - name: Restart Services
        shell: bash
        env:
          DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
          DEPLOY_USER: ${{ secrets.DEPLOY_USER }}
          DEPLOY_PORT: ${{ secrets.DEPLOY_PORT }}
          DEPLOY_SSH_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
        run: |
          set -euo pipefail
          
          ssh -i ~/.ssh/deploy_key -p "$DEPLOY_PORT" "${DEPLOY_USER}@${DEPLOY_HOST}" "sudo systemctl restart api.service"
          ssh -i ~/.ssh/deploy_key -p "$DEPLOY_PORT" "${DEPLOY_USER}@${DEPLOY_HOST}" "unzip -o /jars/current/sw.zip -d /jars"
