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

parameters:
  - name: serviceName
    type: string
    default: serviceName
  - name: serviceBase
    type: object
    default:
      image: baseImage
      tag: baseImageTag
  - name: servicePool
    type: string
    default: 'Prod Private Agent Pool'
  - name: serviceTimeout
    type: number
    default: 60
  # extraBuildOptions: ''
  - name: serviceDependencies
    type: object
    default: []
  - name: serviceArtifacts
    type: string
    default: ''
  - name: buildSteps
    type: object
    default:
      - dependencies
      - compile
      - lint
      - test

stages:
  - stage: ${{ parameters.serviceName }}
    dependsOn: ${{ parameters.serviceDependencies }}
    pool: '${{ parameters.servicePool }}'
    jobs:
    - job: ImageBuild_${{parameters.serviceName}}
      displayName: Build ${{parameters.serviceName}} image
      condition: or(eq(variables.forcePushLatest, true), eq(variables.onMasterBranch, true), eq(variables.onUpdateBranch, true))
      timeoutInMinutes: ${{ parameters.serviceTimeout }}
      container:
        image: devfra.azurecr.io/de.fraport.build/tools:1.1.0
        endpoint: devfra
      steps:
        - checkout: self
        - task: Docker@2
          displayName: Login to container registry
          inputs:
            command: login
            containerRegistry: devFra
        - script: |
            ls -a .
            pwd
            find .
        - task: Bash@3
          displayName: Build ${{parameters.serviceName}} image
          inputs:
            targetType: inline
            script: |
              cp docker/${{parameters.serviceName}}/Dockerfile .
              docker build \
              --tag $(imageUpstream)/${{parameters.serviceName}}:$(Build.BuildNumber) \
              --build-arg FROM_IMG=${{parameters.serviceBase.image}} \
              --build-arg FROM_TAG=${{parameters.serviceBase.tag}} \
              --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 PROJECT_DIR=$(Build.Repository.LocalPath) \
              --build-arg IN_CI=true \
              --build-arg IN_CONTAINER=true \
              .
        - task: Bash@3
          displayName: Push ${{ parameters.serviceName }} image
          inputs:
            targetType: inline
            script: |
              docker push $(imageUpstream)/${{parameters.serviceName}}:$(Build.BuildNumber)
        - task: Bash@3
          displayName: Update latest ${{parameters.serviceName}} image
          condition: or(eq(variables.forcePushLatest, true), eq(variables.onMasterBranch, true))
          inputs:
            targetType: inline
            script: |
              docker tag $(imageUpstream)/${{parameters.serviceName}}:$(Build.BuildNumber) $(imageUpstream)/${{parameters.serviceName}}:latest
              docker push $(imageUpstream)/${{parameters.serviceName}}:latest
        - task: Docker@2
          displayName: Logout from container registry
          inputs:
            command: logout
            containerRegistry: devFra

    - job: Build_${{parameters.serviceName}}
      displayName: Build ${{parameters.serviceName}}
      dependsOn:
        - ImageBuild_${{parameters.serviceName}}
      condition: in(dependencies.ImageBuild_${{parameters.serviceName}}.result, 'Succeeded', 'Skipped')
      container:
        # TODO: use BuildNumber instead of latest in update branches
        # image: devfra.azurecr.io/de.fraport.fradrive.build/frontend:$(Build.BuildNumber)
        image: $(imageUpstream)/${{parameters.serviceName}}:latest
        endpoint: devfra
        env:
          PROJECT_DIR: $(Build.Repository.LocalPath)
          IN_CONTAINER: true
          IN_CI: true
      steps:
        - checkout: self
        - ${{ each dep in parameters.serviceDependencies }}:
          - download: current
            artifact: ${{ dep }}
        - ${{ each buildStep in parameters.buildSteps }}:
          - template: ./service/build-step.yaml
            parameters:
              service: ${{ parameters.serviceName }}
              buildStep: ${{ buildStep }}
        - task: CopyFiles@2
          displayName: Copy ${{parameters.serviceName}} artifacts
          inputs:
            Contents: ${{ parameters.serviceArtifacts }}
            TargetFolder: '$(Build.ArtifactStagingDirectory)'
        - task: PublishBuildArtifacts@1
          displayName: Publish ${{parameters.serviceName}} artifacts
          inputs:
            PathtoPublish: '$(Build.ArtifactStagingDirectory)'
            ArtifactName: '${{parameters.serviceName}}'
            publishLocation: 'Container'