trigger:
  branches:
    include:
      - main
      - develop
  paths:
    exclude:
      - README.md
      - docs/

pr:
  branches:
    include:
      - main

schedule:
  - cron: "0 6 * * *"
    displayName: "Nightly tests"
    branches:
      include:
        - main

variables:
  pythonVersion: '3.12'
  venomqaReportDir: 'reports'
  ${{ if eq(variables['Build.SourceBranch'], 'refs/heads/main') }}:
    environment: 'production'
  ${{ else }}:
    environment: 'staging'

stages:
  - stage: Lint
    displayName: 'Code Quality'
    jobs:
      - job: Lint
        pool:
          vmImage: 'ubuntu-latest'
        steps:
          - task: UsePythonVersion@0
            inputs:
              versionSpec: '$(pythonVersion)'
              addToPath: true
          
          - script: |
              pip install ruff mypy
            displayName: 'Install linters'
          
          - script: |
              ruff check . --output-format=azure
            displayName: 'Run Ruff'
            continueOnError: true
          
          - script: |
              mypy . --ignore-missing-imports || true
            displayName: 'Run MyPy'
            continueOnError: true

  - stage: UnitTests
    displayName: 'Unit Tests'
    dependsOn: Lint
    jobs:
      - job: UnitTests
        pool:
          vmImage: 'ubuntu-latest'
        steps:
          - task: UsePythonVersion@0
            inputs:
              versionSpec: '$(pythonVersion)'
              addToPath: true
          
          - script: |
              pip install -e ".[dev]"
            displayName: 'Install VenomQA'
          
          - script: |
              pytest tests/unit \
                -v \
                --junitxml=$(venomqaReportDir)/unit-junit.xml \
                --cov=venomqa \
                --cov-report=xml:$(venomqaReportDir)/coverage.xml \
                --cov-report=html:$(venomqaReportDir)/coverage-html
            displayName: 'Run unit tests'
          
          - task: PublishTestResults@2
            inputs:
              testResultsFormat: 'JUnit'
              testResultsFiles: '$(venomqaReportDir)/*-junit.xml'
              testRunTitle: 'Unit Tests'
            condition: succeededOrFailed()
          
          - task: PublishCodeCoverageResults@2
            inputs:
              summaryFileLocation: '$(venomqaReportDir)/coverage.xml'
              reportDirectory: '$(venomqaReportDir)/coverage-html'
            condition: succeededOrFailed()

  - stage: APITests
    displayName: 'API Integration Tests'
    dependsOn: Lint
    jobs:
      - job: APITests
        pool:
          vmImage: 'ubuntu-latest'
        services:
          postgres:
            image: postgres:15-alpine
            ports:
              - 5432:5432
            env:
              POSTGRES_USER: venomqa
              POSTGRES_PASSWORD: venomqa_pass
              POSTGRES_DB: venomqa_test
          redis:
            image: redis:7-alpine
            ports:
              - 6379:6379
        variables:
          DATABASE_URL: 'postgresql://venomqa:venomqa_pass@localhost:5432/venomqa_test'
          REDIS_URL: 'redis://localhost:6379/0'
          API_BASE_URL: 'http://localhost:8000'
        steps:
          - task: UsePythonVersion@0
            inputs:
              versionSpec: '$(pythonVersion)'
              addToPath: true
          
          - script: |
              pip install -e ".[dev]"
            displayName: 'Install VenomQA'
          
          - script: |
              python -m http.server 8000 --directory tests/fixtures/mock_api &
              sleep 5
            displayName: 'Start mock API'
          
          - script: |
              for i in $(seq 1 30); do
                if curl -sf http://localhost:8000/health >/dev/null 2>&1; then
                  echo "API ready"
                  break
                fi
                sleep 2
              done
            displayName: 'Wait for API'
          
          - script: |
              venomqa run \
                --config venomqa.yaml \
                --output-dir $(venomqaReportDir) \
                --format html json junit \
                --parallel 4
            displayName: 'Run VenomQA'
            env:
              API_BASE_URL: $(API_BASE_URL)
              DATABASE_URL: $(DATABASE_URL)
              REDIS_URL: $(REDIS_URL)
          
          - task: PublishTestResults@2
            inputs:
              testResultsFormat: 'JUnit'
              testResultsFiles: '$(venomqaReportDir)/*-junit.xml'
              testRunTitle: 'API Integration Tests'
            condition: succeededOrFailed()
          
          - task: PublishBuildArtifacts@1
            inputs:
              pathToPublish: '$(venomqaReportDir)'
              artifactName: 'api-test-reports'
            condition: succeededOrFailed()

  - stage: E2ETests
    displayName: 'E2E Tests'
    dependsOn:
      - UnitTests
      - APITests
    condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main'))
    jobs:
      - job: E2ETests
        pool:
          vmImage: 'ubuntu-latest'
        steps:
          - task: UsePythonVersion@0
            inputs:
              versionSpec: '$(pythonVersion)'
              addToPath: true
          
          - task: DockerInstaller@0
            inputs:
              dockerVersion: '24.0'
          
          - script: |
              pip install -e ".[dev]"
            displayName: 'Install VenomQA'
          
          - script: |
              docker-compose -f docker-compose.qa.yml up -d
            displayName: 'Start services'
          
          - script: |
              for i in $(seq 1 60); do
                if curl -sf http://localhost:8000/health >/dev/null 2>&1; then
                  echo "API ready"
                  exit 0
                fi
                echo "Waiting... ($i/60)"
                sleep 5
              done
              exit 1
            displayName: 'Wait for services'
          
          - script: |
              venomqa run \
                --config venomqa.yaml \
                --journeys-dir journeys \
                --output-dir $(venomqaReportDir) \
                --format html json junit sarif \
                --tags e2e \
                --parallel 2
            displayName: 'Run E2E tests'
          
          - script: |
              docker-compose -f docker-compose.qa.yml down -v
            displayName: 'Cleanup'
            condition: succeededOrFailed()
          
          - task: PublishTestResults@2
            inputs:
              testResultsFormat: 'JUnit'
              testResultsFiles: '$(venomqaReportDir)/*-junit.xml'
              testRunTitle: 'E2E Tests'
            condition: succeededOrFailed()
          
          - task: PublishBuildArtifacts@1
            inputs:
              pathToPublish: '$(venomqaReportDir)'
              artifactName: 'e2e-test-reports'
            condition: succeededOrFailed()

  - stage: PerformanceTests
    displayName: 'Performance Tests'
    dependsOn:
      - UnitTests
      - APITests
    condition: and(succeeded(), eq(variables['Build.Reason'], 'Schedule'))
    jobs:
      - job: PerformanceTests
        pool:
          vmImage: 'ubuntu-latest'
        steps:
          - task: UsePythonVersion@0
            inputs:
              versionSpec: '$(pythonVersion)'
              addToPath: true
          
          - script: |
              pip install -e ".[dev,performance]"
            displayName: 'Install VenomQA with performance extras'
          
          - script: |
              venomqa run \
                --config venomqa.yaml \
                --journeys-dir journeys \
                --output-dir $(venomqaReportDir) \
                --tags performance \
                --parallel 8 \
                --iterations 100
            displayName: 'Run performance tests'
          
          - task: PublishBuildArtifacts@1
            inputs:
              pathToPublish: '$(venomqaReportDir)'
              artifactName: 'performance-reports'

  - stage: Notify
    displayName: 'Send Notifications'
    dependsOn:
      - UnitTests
      - APITests
      - E2ETests
    condition: succeededOrFailed()
    jobs:
      - job: Notify
        pool:
          vmImage: 'ubuntu-latest'
        steps:
          - checkout: none
          
          - task: SlackNotification@2
            inputs:
              SlackConnection: 'SlackConnection'
              ChannelIdentifier: '#qa-notifications'
              MessageSeverity: '$(Build.Status)'
              NotificationMessage: |
                VenomQA Pipeline $(Build.Status)
                Repository: $(Build.Repository.Name)
                Branch: $(Build.SourceBranchName)
                Commit: $(Build.SourceVersion)
                Build: $(Build.BuildNumber)
                Link: $(Build.BuildUri)
