# SPDX-FileCopyrightText: 2024-2025 Sarah Vaupel <sarah.vaupel@uniworx.de>
#
# SPDX-License-Identifier: AGPL-3.0-or-later

pool: 'Prod Private Agent Pool'

variables:
  imageFradriveUpstream: devfra.azurecr.io/de.fraport.fradrive.build
  onMasterBranch: $[eq(variables['Build.SourceBranch'], 'refs/heads/master')]
  onUpdateBranch: $[startsWith(variables['Build.SourceBranch'], 'refs/heads/update')]

jobs:
- job: FrontendImage
  condition: or(eq(variables.onMasterBranch, true), eq(variables.onUpdateBranch, true))
  container:
    image: devfra.azurecr.io/de.fraport.build/tools:1.1.0
    endpoint: devfra
  steps:
    - checkout: self
    - task: Docker@2
      name: dockerLoginDevFra
      displayName: Docker Login to devfra
      inputs:
        command: login
        containerRegistry: devFra
    - task: Bash@3
      displayName: Build frontend image
      inputs:
        targetType: inline
        script: |
          cp docker/frontend/Dockerfile .
          docker build \
          --build-arg FROM_IMG=devfra.azurecr.io/de.fraport.build/npm \
          --build-arg FROM_TAG=node-20 \
          --build-arg HTTPS_PROXY=http://proxy.frankfurt-airport.de:8080 \
          --build-arg HTTP_PROXY=http://proxy.frankfurt-airport.de:8080 \
          --build-arg NO_PROXY='localhost,127.0.0.1,*.docker.internal,*.azmk8s.io,devfra.azurecr.io,devfra.westeurope.data.azurecr.io' \
          --build-arg FRAPORT_NOPROXY=dev.azure.com,*.dev.azure.com,*.fraport.de,*.frankfurt-airport.de \
          --build-arg NPM_CUSTOM_REGISTRY=https://pkgs.dev.azure.com/fraport/_packaging/packages/npm/registry/ \
          --build-arg MOUNT_DIR=$(Build.Repository.LocalPath) \
          --build-arg PROJECT_DIR=/fradrive \
          --build-arg IN_CI=true \
          --tag ${{variables.imageFradriveUpstream}}/frontend:$(Build.BuildNumber) \
          .
    - task: Bash@3
      displayName: Push frontend image
      inputs:
        targetType: inline
        script: |
          docker push ${{variables.imageFradriveUpstream}}/frontend:$(Build.BuildNumber)
    - task: Bash@3
      displayName: Update latest frontend image
      condition: eq(variables.onMasterBranch, true)
      inputs:
        targetType: inline
        script: |
          docker tag ${{variables.imageFradriveUpstream}}/frontend:$(Build.BuildNumber) ${{variables.imageFradriveUpstream}}/frontend:latest
          docker push ${{variables.imageFradriveUpstream}}/frontend:latest
    - task: Docker@2
      name: dockerLogoutDevFra
      displayName: Docker Logout from devfra
      inputs:
        command: logout
        containerRegistry: devFra

- job: FrontendBuild
  # TODO: wait for FrontendImage iff the job was executed
  # dependsOn: FrontendImage
  # condition: succeeded()
  container:
    # image: devfra.azurecr.io/de.fraport.fradrive.build/frontend:$(Build.BuildNumber)
    image: devfra.azurecr.io/de.fraport.fradrive.build/frontend:latest
    endpoint: devfra
    env:
      PROJECT_DIR: $(Build.Repository.LocalPath)
      IN_CONTAINER: true
      IN_CI: true
  steps:
    - checkout: self
    - task: Bash@3
      name: frontendDependencies
      displayName: Install frontend dependencies
      env:
        HTTPS_PROXY: http://proxy.frankfurt-airport.de:8080
        HTTP_PROXY: http://proxy.frankfurt-airport.de:8080
        NO_PROXY: 'localhost,127.0.0.1,*.docker.internal,*.azmk8s.io,devfra.azurecr.io,devfra.westeurope.data.azurecr.io'
        FRAPORT_NOPROXY: 'dev.azure.com,*.dev.azure.com,*.fraport.de,*.frankfurt-airport.de'
        PROJECT_DIR: $(Build.Repository.LocalPath)
      inputs:
        targetType: inline
        script: |
          make -- --dependencies-frontend IN_CONTAINER=true IN_CI=true PROJECT_DIR=${PROJECT_DIR}
    - task: Bash@3
      name: frontendCompile
      displayName: Compile frontend
      env:
        PROJECT_DIR: $(Build.Repository.LocalPath)
      inputs:
        targetType: inline
        script: |
          make -- --compile-frontend IN_CONTAINER=true IN_CI=true PROJECT_DIR=${PROJECT_DIR}
    - task: Bash@3
      name: frontendTest
      displayName: Test frontend
      inputs:
        targetType: inline
        script: |
          make -- --lint-frontend IN_CONTAINER=true
          make -- --test-frontend IN_CONTAINER=true
    - task: CopyFiles@2
      name: frontendCopyArtifacts
      displayName: Prepare frontend artifacts
      inputs:
        Contents: |
          static/
          well-known/
          frontend/src/env.sass
          config/manifest.json
        TargetFolder: '$(Build.ArtifactStagingDirectory)'
    - task: PublishBuildArtifacts@1
      name: frontendPublishArtifacts
      displayName: Publish frontend artifacts
      inputs:
        PathtoPublish: '$(Build.ArtifactStagingDirectory)'
        ArtifactName: 'frontend'
        publishLocation: 'Container'