trigger:
  branches:
    include:
      - main
      - develop

pr:
  branches:
    include:
      - main
      - develop

variables:
  javaVersion: '21'
  nodeVersion: '20'
  mavenCacheFolder: $(Pipeline.Workspace)/.m2/repository
  npmCacheFolder: $(Pipeline.Workspace)/.npm

stages:
  - stage: Build
    displayName: Build Stage
    jobs:
      - job: BuildBackend
        displayName: Build Backend Services
        pool:
          vmImage: 'ubuntu-latest'
        steps:
          - task: Cache@2
            displayName: Cache Maven packages
            inputs:
              key: 'maven | "$(Agent.OS)" | **/pom.xml'
              restoreKeys: |
                maven | "$(Agent.OS)"
              path: $(mavenCacheFolder)

          - task: JavaToolInstaller@0
            displayName: Install Java $(javaVersion)
            inputs:
              versionSpec: $(javaVersion)
              jdkArchitectureOption: x64
              jdkSourceOption: PreInstalled

          - task: Maven@4
            displayName: Build qa-service-user
            inputs:
              mavenPomFile: 'server/qa-service-user/pom.xml'
              goals: 'clean package -DskipTests'
              mavenOptions: '-Xmx3072m -Dmaven.repo.local=$(mavenCacheFolder)'
              javaHomeOption: 'JDKVersion'
              jdkVersionOption: $(javaVersion)
              jdkArchitectureOption: x64

          - task: Maven@4
            displayName: Build qa-service-question
            inputs:
              mavenPomFile: 'server/qa-service-question/pom.xml'
              goals: 'clean package -DskipTests'
              mavenOptions: '-Xmx3072m -Dmaven.repo.local=$(mavenCacheFolder)'
              javaHomeOption: 'JDKVersion'
              jdkVersionOption: $(javaVersion)
              jdkArchitectureOption: x64

          - task: CopyFiles@2
            displayName: Copy Backend Artifacts
            inputs:
              SourceFolder: 'server'
              Contents: |
                **/target/*.jar
              TargetFolder: $(Build.ArtifactStagingDirectory)/backend

          - task: PublishBuildArtifacts@1
            displayName: Publish Backend Artifacts
            inputs:
              PathtoPublish: $(Build.ArtifactStagingDirectory)/backend
              ArtifactName: backend
              publishLocation: Container

      - job: BuildFrontend
        displayName: Build Frontend
        pool:
          vmImage: 'ubuntu-latest'
        steps:
          - task: NodeTool@0
            displayName: Install Node.js $(nodeVersion)
            inputs:
              versionSpec: $(nodeVersion)

          - task: Cache@2
            displayName: Cache npm packages
            inputs:
              key: 'npm | "$(Agent.OS)" | web/qa-web/package.json'
              restoreKeys: |
                npm | "$(Agent.OS)"
              path: $(npmCacheFolder)

          - script: |
              cd web/qa-web
              npm ci --cache $(npmCacheFolder)
            displayName: npm ci

          - script: |
              cd web/qa-web
              npm run build
            displayName: Build Frontend (Vite)

          - task: CopyFiles@2
            displayName: Copy Frontend Artifacts
            inputs:
              SourceFolder: 'web/qa-web/dist'
              Contents: '**'
              TargetFolder: $(Build.ArtifactStagingDirectory)/frontend

          - task: PublishBuildArtifacts@1
            displayName: Publish Frontend Artifacts
            inputs:
              PathtoPublish: $(Build.ArtifactStagingDirectory)/frontend
              ArtifactName: frontend
              publishLocation: Container

  - stage: Test
    displayName: Test Stage
    dependsOn: Build
    jobs:
      - job: TestBackend
        displayName: Test Backend Services
        pool:
          vmImage: 'ubuntu-latest'
        steps:
          - task: JavaToolInstaller@0
            displayName: Install Java $(javaVersion)
            inputs:
              versionSpec: $(javaVersion)
              jdkArchitectureOption: x64
              jdkSourceOption: PreInstalled

          - task: Maven@4
            displayName: Test qa-service-user
            inputs:
              mavenPomFile: 'server/qa-service-user/pom.xml'
              goals: 'test'
              mavenOptions: '-Xmx3072m -Dmaven.repo.local=$(mavenCacheFolder)'
              javaHomeOption: 'JDKVersion'
              jdkVersionOption: $(javaVersion)
              jdkArchitectureOption: x64
              publishJUnitResults: true
              testResultsFiles: 'server/qa-service-user/target/surefire-reports/**/*.xml'

          - task: Maven@4
            displayName: Test qa-service-question
            inputs:
              mavenPomFile: 'server/qa-service-question/pom.xml'
              goals: 'test'
              mavenOptions: '-Xmx3072m -Dmaven.repo.local=$(mavenCacheFolder)'
              javaHomeOption: 'JDKVersion'
              jdkVersionOption: $(javaVersion)
              jdkArchitectureOption: x64
              publishJUnitResults: true
              testResultsFiles: 'server/qa-service-question/target/surefire-reports/**/*.xml'

          - task: PublishTestResults@2
            displayName: Publish Backend Test Results
            inputs:
              testResultsFiles: 'server/**/target/surefire-reports/**/*.xml'
              mergeTestResults: true
              testRunTitle: 'Backend Unit Tests'
            condition: succeededOrFailed()

      - job: TestFrontend
        displayName: Test Frontend
        pool:
          vmImage: 'ubuntu-latest'
        steps:
          - task: NodeTool@0
            displayName: Install Node.js $(nodeVersion)
            inputs:
              versionSpec: $(nodeVersion)

          - script: |
              cd web/qa-web
              npm ci
            displayName: npm ci

          - script: |
              cd web/qa-web
              npm run type-check || true
            displayName: Type Check (vue-tsc)
            continueOnError: true
