stages:
  - backend build
  - frontend build
  - backend test
  - frontend test
  - deploy

cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
    - .m2/repository/
    - target/
    - .yarn
    - android-sdk/
    - .gradle/wrapper
    - .gradle/caches

variables:
  # Specify the SDK tools version and build tools version to use
  ANDROID_COMPILE_SDK: 33
  ANDROID_BUILD_TOOLS: 33.0.0
  ANDROID_SDK_TOOLS: 7583922
  ANDROID_HOME: "/usr/local/android-sdk"
  GIT_STRATEGY: clone


Prettier:
  image: node:16.10.0
  stage: backend test
  needs: []
  allow_failure: true
  script:
    - cd backend
    - npm i
    - npx prettier --check .
  retry: 1

Typescript-compile:
  image: node:16.10.0
  stage: backend build
  needs: []
  script:
      - cd backend
      - yarn
      - yarn tsc

API-test:
  image: cypress/base:14.17.0
  stage: backend test
  needs: [Typescript-compile]
  variables:
    DISPLAY: ":99"
  script:
      - apt-get update && apt-get install -y xvfb
      - Xvfb :99 -screen 0 1920x1080x24 &
      - cd backend
      - echo "FB_PRIV_KEY_JSON_ENCODED=$FB_PRIV_KEY_JSON_ENCODED" >> .env
      # - echo "export FB_PRIV_KEY_JSON_ENCODED=$FB_PRIV_KEY_JSON_ENCODED"
      - node keys/decode.js
      - chmod 400 keys/fb-key.json
      - yarn
      - yarn start:local & sleep 10
      - yarn test
  retry: 1
  artifacts:
    paths:
      - cypress/videos/*.mp4
    expire_in: 1 week # Set the artifact expiration time according to your needs
  only:
    refs:
      - main




Gradle-build:
  image: gradle:7.5.0-jdk11
  stage: frontend test
  needs: []
  script:
      # Restore Android SDK from cache
  #- if [ -d android-sdk ]; then mv android-sdk/* $ANDROID_HOME/; fi
  # Download and install Android SDK
  - wget --quiet --output-document=android-sdk.zip https://dl.google.com/android/repository/commandlinetools-linux-${ANDROID_SDK_TOOLS}_latest.zip
  - unzip -q -o android-sdk.zip -d android-sdk
  - rm -rf $ANDROID_HOME/platforms/android-33
  - echo y | android-sdk/cmdline-tools/bin/sdkmanager --sdk_root=$ANDROID_HOME "platforms;android-${ANDROID_COMPILE_SDK}" "build-tools;${ANDROID_BUILD_TOOLS}"
  - cd frontend
  - ./gradlew clean
  - ./gradlew build --refresh-dependencies
  - gradle build

VM Deploy:
  image: node:16.10.0
  stage: deploy
  needs: [Typescript-compile, API-test]
  before_script:
    - mkdir -p ~/.ssh
    - echo "$NTNU_VM_SSH_KEY" | tr -d '\r' > ~/.ssh/id_rsa
    - chmod 600 ~/.ssh/id_rsa
    - ssh-keyscan 10.212.26.72 >> ~/.ssh/known_hosts
    - ssh-keygen -p -f ~/.ssh/id_rsa

  environment:
    name: production
    url: http://10.212.26.72/

  script:
    - ssh -v -i ~/.ssh/id_rsa -o StrictHostKeyChecking=no ubuntu@10.212.26.72 -p 22 "bash /home/git/tdt4240-tank-wars/backend/vm.sh </dev/null >/dev/null 2>&1"
    
  only:
    refs:
      - main

VM Heartbeat:
  stage: deploy
  needs: [VM Deploy]
  image: alpine
  script:
    - apk add --no-cache curl
    - |
      for i in {1..18}; do
        sleep 10
        response_code=$(curl --silent --write-out "%{http_code}\n" --output response.txt --connect-timeout 5 --max-time 10 http://10.212.26.72/server/heartbeat)
        response_body=$(cat response.txt)
        echo "Response code: $response_code"
        echo "Response body: $response_body"
        if [ "$response_code" -eq 200 ]; then
          exit 0
        fi
      done
      echo "Server did not respond with 200 OK after 3 minutes"
      exit 1
  rules:
    - if: '$CI_COMMIT_BRANCH == "main"'
    - if: '$CI_PIPELINE_SOURCE == "schedule" && $RUN_VM_HEARTBEAT == "true"'