trigger:
  branches:
    include:
      - "*"

pool:
  vmImage: ubuntu-latest

variables:
  - group: az-cli

resources:
  repositories:
    - repository: cicd-tools
      type: git
      name: SGClima-Data-Engineering/cicd-tools

jobs:
    - job: Test
      displayName: Testing as locally by developer...
      steps:
        - task: UsePythonVersion@0
          inputs:
            versionSpec: '3.13'
            addToPath: true

        - checkout: self
          path: s
          displayName: Checkout primary repository

        - checkout: cicd-tools

        - script:  cicd-tools/cicd install self
          displayName: Install cicd-tools & clean repo

        - bash: curl -LsSf https://astral.sh/uv/install.sh | sh
          displayName: Install uv to the project management

        - task: AzureCLI@2
          displayName: "Run tests with Azure CLI authentication"
          inputs:
            azureSubscription: 'sgcdataeng-keyvault-conn'
            scriptType: bash
            scriptLocation: inlineScript
            inlineScript: |
              uv sync --all-groups
              uv run pytest -vv

    - job: MergeToMain
      dependsOn: Test
      condition: and(succeeded(), eq(variables['Build.SourceBranchName'], 'release'))
      steps:
        - task: UsePythonVersion@0
          inputs:
            versionSpec: '3.13'
            addToPath: true
        - checkout: self
          path: s
          persistCredentials: true
          fetchDepth: 0  # Add this to get full history
          displayName: Checkout primary repository

        - checkout: cicd-tools

        - script: cicd-tools/cicd install self
          displayName: Install cicd-tools & clean repo

        - script: cicd build merge2main
          displayName: 'Merge release branch into main'
          workingDirectory: $(Pipeline.Workspace)/s

    - job: Publish
     # dependsOn: Test
      displayName: Fix release and publish to pypi
      variables:
        System.AccessToken: $(System.AccessToken)
      condition: and(succeeded(), eq(variables['Build.SourceBranchName'], 'main'))
      steps:
        - task: UsePythonVersion@0
          inputs:
            versionSpec: '3.13'
            addToPath: true


        - checkout: self
          path: s
          persistCredentials: true
          fetchDepth: 0
          displayName: Checkout primary repository

        - checkout: cicd-tools

        - script:  cicd-tools/cicd install self
          displayName: Install cicd-tools & clean repo

        - script: cicd install commitizen
          displayName: Install commitizen for versioning control

        - bash: curl -LsSf https://astral.sh/uv/install.sh | sh
          displayName: Install uv to the project

        - script: cicd build bump
          displayName: Bump a version number from the commits

        - script: uv build
          displayName: Build the package

        
        - script: |
            uv publish \
            --username azure \
            --password $(System.AccessToken) \
            --publish-url https://pkgs.dev.azure.com/Indoorclima/SGClima-Data-Engineering/_packaging/savcfg/pypi/upload \
            dist/*
          displayName: Publish to Azure Artifacts
          env:
            PYPI_TOKEN: $(System.AccessToken)

        - script: cicd build push2git
          displayName: Push to git repository the release
