parameters:
- name: ServiceDirectory
  type: string
  default: not-specified
- name: CtestRegex
  type: string
  default: .*
- name: Coverage
  type: string
  default: 'enabled'
- name: LiveTestCtestRegex
  type: string
  default: ''
- name: Artifacts
  type: object
  default: []
- name: Location
  type: string
  default: ''
- name: SubscriptionConfiguration
  type: string
  default: $(sub-config-azure-cloud-test-resources)
- name: CoverageReportPath
  type: string
  default: 'sdk/*/*/*cov_xml.xml'
- name: LiveTestTimeoutInMinutes
  type: number
  default: 60

stages:
  - stage: Build
    jobs:
      - template: /eng/pipelines/templates/jobs/archetype-sdk-client.yml
        parameters:
          ServiceDirectory: ${{ parameters.ServiceDirectory }}
          Artifacts: ${{ parameters.Artifacts }}
          CtestRegex: ${{ parameters.CtestRegex }}
          Coverage: ${{ parameters.Coverage }}
          CoverageReportPath: ${{ parameters.CoverageReportPath }}
          ${{ if eq(parameters.ServiceDirectory, 'template') }}:
            TestPipeline: true

  - ${{ if and(eq(variables['System.TeamProject'], 'internal'), ne(parameters.LiveTestCtestRegex, '')) }}:
    - stage: LiveTest
      dependsOn: []
      jobs:
        - template: /eng/pipelines/templates/jobs/archetype-sdk-tests.yml
          # Code coverage is enabled by default for live tests
          parameters:
            TimeoutInMinutes: ${{ parameters.LiveTestTimeoutInMinutes }}
            ServiceDirectory: ${{ parameters.ServiceDirectory }}
            CtestRegex: ${{ parameters.LiveTestCtestRegex }}
            Location: ${{ parameters.Location }}
            SubscriptionConfiguration: ${{ parameters.SubscriptionConfiguration }}
            CoverageReportPath: ${{ parameters.CoverageReportPath }}

  - ${{ if and(eq(variables['System.TeamProject'], 'internal'), not(endsWith(variables['Build.DefinitionName'], ' - tests'))) }}:
    - template: archetype-cpp-release.yml
      parameters:
        ServiceDirectory: ${{ parameters.ServiceDirectory }}
        DependsOn:
          - Build
          # Only depend on `LiveTest` if there are live tests to execute
          - ${{ if and(eq(variables['System.TeamProject'], 'internal'), ne(parameters.LiveTestCtestRegex, '')) }}:
            - LiveTest
        Artifacts: ${{ parameters.Artifacts }}
        ArtifactName: packages
        ${{ if eq(parameters.ServiceDirectory, 'template') }}:
          TestPipeline: true
