name: SPT-Server Nightly Manual

on:
  push:
    paths:
      - '.github/workflows/build-nightly-manual.yaml'

env:
  SERVER_URL: https://dev.sp-tarkov.com
  REPOSITORY_SERVER: SPT/Server
  REPOSITORY_SERVER_MEDUSA: medusa/spt-server
  NIGHTLY_BRANCH: 3.11.x-DEV

jobs:
  prepare:
    runs-on: ubuntu-latest
    outputs:
      SPT_VERSION: ${{ steps.versions.outputs.SPT_VERSION }}
      EFT_VERSION: ${{ steps.versions.outputs.EFT_VERSION }}
      BUILD_DATE_TIME: ${{ steps.versions.outputs.DATE_TIME }}

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Extract versions
        id: versions
        run: |
          # Extract versions from core.json
          wget ${{ env.SERVER_URL }}/${{ env.REPOSITORY_SERVER_MEDUSA }}/raw/branch/${{ env.NIGHTLY_BRANCH }}/project/assets/configs/core.json
          SPT_VERSION=$(jq -r '.sptVersion' core.json)
          EFT_VERSION=$(jq -r '.compatibleTarkovVersion' core.json)

          echo "👽 SPT_VERSION = $SPT_VERSION"
          echo "👽 EFT_VERSION = $EFT_VERSION"
          echo "SPT_VERSION=$SPT_VERSION" >> $GITHUB_OUTPUT
          echo "EFT_VERSION=$EFT_VERSION" >> $GITHUB_OUTPUT
          echo "DATE_TIME=$(date +%Y%m%d%H%M%S)" >> $GITHUB_OUTPUT
        shell: bash

  build-server-windows:
    needs: prepare
    runs-on: windows-latest
    env:
      OUTPUT_DIR: spt-server
      SOURCECODE_DIR: c:/code

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Setup Git Config
        run: |
          git config --global user.name "github-actions[bot]"
          git config --global user.email "bot@github.com"

      - name: Clone Medusa's Server Code
        run: |
          git clone ${{ env.SERVER_URL }}/${{ env.REPOSITORY_SERVER_MEDUSA }} ${{ env.SOURCECODE_DIR }}
          cd ${{ env.SOURCECODE_DIR }}
          git checkout ${{ env.NIGHTLY_BRANCH }}_windows
          git lfs pull
        shell: bash

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 20.11.1

      - name: Runner Debug Information
        id: debug-info
        run: |
          cp -v ${{ env.SOURCECODE_DIR }}/project/package.json .
          cd ${{ env.SOURCECODE_DIR }}
          echo "git version: $(git --version)"
          echo "git lfs version: $(git-lfs --version)"
          echo "node.js version: $(node --version)"
          echo "npm version: $(npm --version)"
          echo "👽 latest commit hash: $(git rev-parse HEAD)"
          echo "👽 last commit message:" && git log -1 --pretty=%B
          echo "COMMIT_ID=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
        shell: bash

      - name: Cache NPM Dependencies
        id: cache-npm-dependencies
        uses: actions/cache@v4
        with:
          path: |
            ${{ env.SOURCECODE_DIR }}/project/node_modules
          key: ${{ runner.os }}-npm-dependencies-${{ hashFiles('package.json') }}
          restore-keys: |
            ${{ runner.os }}-npm-dependencies-

      - name: Install NPM Dependencies
        if: steps.cache-npm-dependencies.outputs.cache-hit != 'true'
        run: |
          cd ${{ env.SOURCECODE_DIR }}/project
          npm install
        shell: pwsh

      - name: Build Server
        id: build-server
        run: |
          cd ${{ env.SOURCECODE_DIR }}/project
          npm run build:release
          ls -l build
          mv build ${{ env.OUTPUT_DIR }}
        shell: bash

      - name: Generate File Name
        id: filename
        run: |
          artifact_name=${{ env.OUTPUT_DIR }}-${{ needs.prepare.outputs.SPT_VERSION }}-win-nightly-${{ steps.debug-info.outputs.COMMIT_ID }}-EFT${{ needs.prepare.outputs.EFT_VERSION }}-${{ needs.prepare.outputs.BUILD_DATE_TIME }}-artifact
          echo "WIN_ARTIFACT=$artifact_name" >> $GITHUB_OUTPUT
        shell: bash

      - name: Artifact Server
        uses: actions/upload-artifact@v4
        with:
          name: ${{ steps.filename.outputs.WIN_ARTIFACT }}
          path: ${{ env.SOURCECODE_DIR }}/project/${{ env.OUTPUT_DIR }}
          overwrite: true

  build-server-linux:
    needs: prepare
    runs-on: ubuntu-latest
    env:
      OUTPUT_DIR: spt-server
      SOURCECODE_DIR: ${{ github.workspace }}/code

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Setup Git Config
        run: |
          git config --global user.name "github-actions[bot]"
          git config --global user.email "bot@github.com"

      - name: Clone Medusa's Server Code
        run: |
          git clone ${{ env.SERVER_URL }}/${{ env.REPOSITORY_SERVER_MEDUSA }} ${{ env.SOURCECODE_DIR }}
          cd ${{ env.SOURCECODE_DIR }}
          git checkout ${{ env.NIGHTLY_BRANCH }}
          git lfs pull
        shell: bash

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: 20.11.1

      - name: Runner Debug Information
        id: debug-info
        run: |
          cp -v ${{ env.SOURCECODE_DIR }}/project/package.json .
          cd ${{ env.SOURCECODE_DIR }}
          echo "git version: $(git --version)"
          echo "git lfs version: $(git-lfs --version)"
          echo "node.js version: $(node --version)"
          echo "npm version: $(npm --version)"
          echo "👽 latest commit hash: $(git rev-parse HEAD)"
          echo "👽 last commit message:" && git log -1 --pretty=%B
          echo "COMMIT_ID=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
        shell: bash

      - name: Cache NPM Dependencies
        id: cache-npm-dependencies
        uses: actions/cache@v4
        with:
          path: |
            ${{ env.SOURCECODE_DIR }}/project/node_modules
          key: ${{ runner.os }}-npm-dependencies-${{ hashFiles('package.json') }}
          restore-keys: |
            ${{ runner.os }}-npm-dependencies-

      - name: Install NPM Dependencies
        if: steps.cache-npm-dependencies.outputs.cache-hit != 'true'
        run: |
          cd ${{ env.SOURCECODE_DIR }}/project
          npm install
        shell: bash

      - name: Build Server
        id: build-server
        run: |
          cd ${{ env.SOURCECODE_DIR }}/project
          npm run build:release
          ls -l build
          mv build ${{ env.OUTPUT_DIR }}
        shell: bash

      - name: Generate File Name
        id: filename
        run: |
          artifact_name=${{ env.OUTPUT_DIR }}-${{ needs.prepare.outputs.SPT_VERSION }}-linux-nightly-${{ steps.debug-info.outputs.COMMIT_ID }}-EFT${{ needs.prepare.outputs.EFT_VERSION }}-${{ needs.prepare.outputs.BUILD_DATE_TIME }}-artifact
          echo "LINUX_ARTIFACT=$artifact_name" >> $GITHUB_OUTPUT
        shell: bash

      - name: Artifact Server
        uses: actions/upload-artifact@v4
        with:
          name: ${{ steps.filename.outputs.LINUX_ARTIFACT }}
          path: ${{ env.SOURCECODE_DIR }}/project/${{ env.OUTPUT_DIR }}
          overwrite: true

