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

parameters:
  - name: serviceName
    type: string
  - name: dependenciesCaches
    type: object
    default: []
  - name: dependenciesBuildPool
    type: string
    default: 'Prod Private Agent Pool'
    values:
      - 'Prod Private Agent Pool'
      - 'Prod Private Agent Pool DS2'
      - 'Prod Private Agent Pool DS3'
  - name: dependenciesBuildCores
    type: number
    default: 1
  - name: dependenciesBuildTimeout
    type: number
    default: 60

jobs:
- job: SetupDependencies_${{parameters.serviceName}}
  displayName: Install ${{parameters.serviceName}} dependencies
  dependsOn: SetupImage_${{parameters.serviceName}}
  ${{ if eq(variables.setupImages, true) }}:
    condition: succeeded()
  ${{ else }}:
    condition: always()
  pool: '${{parameters.dependenciesBuildPool}}'
  timeoutInMinutes: ${{parameters.dependenciesBuildTimeout}}
  container:
    ${{ if variables.setupImages }}:
      image: $(buildImageUpstream)/${{parameters.serviceName}}:$(Build.BuildNumber)
    ${{ else }}:
      image: $(buildImageUpstream)/${{parameters.serviceName}}:latest
    endpoint: devfra
    env:
      PROJECT_DIR: $(Build.Repository.LocalPath)
      IN_CONTAINER: true
      IN_CI: true
  steps:
    # Restore previously-built dependencies from caches
    - ${{ each cache in parameters.dependenciesCaches }}:
      - template: ./../../steps/cache.yaml
        parameters:
          cacheIdent: '${{parameters.serviceName}}-dependencies'
          cacheKeys: '${{cache.key}}'
          cachePath: '${{cache.path}}'

    # Compile dependencies
    - template: ./../../steps/make.yaml
      parameters:
        makeJob: dependencies
        makeService: ${{parameters.serviceName}}
        makeVars: 'CPU_CORES=${{parameters.dependenciesBuildCores}} STACK_CORES=-j${{parameters.dependenciesBuildCores}}'

    # (Note: a post-job for updating the dependency cache is automatically created, so no further step is due here.)