trigger:
- release/*

schedules:
- cron: "0 2 * * 1-5"  # Run at 2 AM every weekday
  displayName: "Weekday Schedule"
  branches:
    include:
    - 'refs/heads/dev'
variables:
  VERSION: ''
  MAJOR_VERSION: ''

jobs:
- job: PrimAITE_Benchmark
  timeoutInMinutes: 360 # 6-hour maximum
  pool:
    name: 'Imaginary Yak Pool'
  workspace:
    clean: all

  steps:
  - checkout: self
    persistCredentials: true

  - script: |
      python3.10 -m venv venv
    displayName: 'Create venv'

  - script: |
      VERSION=$(cat src/primaite/VERSION | tr -d '\n')
      if [[ "$(Build.SourceBranch)" == "refs/heads/dev" ]]; then
        DATE=$(date +%Y%m%d)
        echo "${VERSION}+dev.${DATE}" > src/primaite/VERSION
      fi
    displayName: 'Update VERSION file for Dev Benchmark'

  - script: |
      VERSION=$(cat src/primaite/VERSION | tr -d '\n')
      MAJOR_VERSION=$(echo $VERSION | cut -d. -f1)
      echo "##vso[task.setvariable variable=VERSION]$VERSION"
      echo "##vso[task.setvariable variable=MAJOR_VERSION]$MAJOR_VERSION"
    displayName: 'Set Version Variables'

  - script: |
      source venv/bin/activate
      pip install --upgrade pip
      pip install -e .[dev,rl]
      primaite setup
    displayName: 'Install Dependencies'

  - script: |
      set -e
      source venv/bin/activate
      cd benchmark
      python primaite_benchmark.py
      cd ..
    displayName: 'Run Benchmarking Script'

  - script: |
      tar czf primaite_v$(VERSION)_benchmark.tar.gz benchmark/results/v$(MAJOR_VERSION)/v$(VERSION)
    displayName: 'Prepare Artifacts for Publishing'

  - task: PublishPipelineArtifact@1
    inputs:
      targetPath: primaite_v$(VERSION)_benchmark.tar.gz
      artifactName: 'benchmark-zip-output'
      publishLocation: 'pipeline'
    displayName: 'Publish Benchmark Output zip as Artifact'

  - script: |
      git config --global user.email "oss@dstl.gov.uk"
      git config --global user.name "Defence Science and Technology Laboratory UK"
    workingDirectory: $(System.DefaultWorkingDirectory)
    displayName: 'Configure Git'
    condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/heads/release'))

  - script: |
      echo "Fetching all branches..."
      git fetch --all --prune

      echo "Stashing files..."
      git stash push -u

      echo "Resolving branch name..."
      # Extracting just the branch name from the full ref path
      branch_name=$(echo "$(Build.SourceBranch)" | sed 's|refs/heads/||')
      echo "Branch Name: $branch_name"

      echo "Checking out branch $branch_name..."
      git checkout $branch_name

      echo "Popping stash..."
      git stash pop

      echo "Adding benchmark results..."
      git add benchmark/results/v$(MAJOR_VERSION)/v$(VERSION)/*

      echo "Committing changes..."
      git commit -m "Automated benchmark output commit for version $(VERSION) [skip ci]"

      echo "Pushing to remote..."
      git push origin $branch_name
    displayName: 'Commit and Push Benchmark Results'
    workingDirectory: $(System.DefaultWorkingDirectory)
    env:
      GIT_CREDENTIALS: $(System.AccessToken)
    condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/heads/release'))
