trigger:
  branches:
    include:
      - main
      - demo-v2
      - demo-v3
      - feature/*

pr:
  branches:
    include:
      - main
      - demo-v2
      - demo-v3
      - feature/*

pool:
  vmImage: ubuntu-latest

variables:
  nodeVersion: '20.x'
  artifactName: sot-demo-v2

stages:
  - stage: Build
    displayName: Build demo artifact
    jobs:
      - job: BuildJob
        displayName: Lint, build, and publish
        steps:
          - task: NodeTool@0
            inputs:
              versionSpec: $(nodeVersion)
            displayName: Install Node.js

          - script: npm ci
            displayName: Install dependencies

          - script: npm run lint
            displayName: Lint

          - script: npm run build
            displayName: Build app

          - script: |
              cat > dist/HOW-TO-RUN.txt <<'EOF'
              SOT Demo v2 — downloaded build artifact
              ==========================================

              This folder is a static build. Do not open index.html directly (file://).
              Run a local web server from this folder:

                npx --yes serve -s . -l 5173

              Then open: http://localhost:5173/

              Demo credentials (see repo README):
                CR: 1010123456
                Seller NID: 1087654321
                Buyer NID: 1102345678

              Full guide: docs/ARTIFACT_DISTRIBUTION.md in the repo.
              EOF
              echo "Build $(Build.BuildId) on $(Build.SourceBranchName)" > dist/build-info.txt
              node -e "const p=require('./package.json'); console.log(JSON.stringify({name:p.name,version:p.version,buildId:process.env.BUILD_BUILDID,branch:process.env.BUILD_SOURCEBRANCHNAME,commit:process.env.BUILD_SOURCEVERSION},null,2))" > dist/build-info.json
            displayName: Stamp build metadata
            env:
              BUILD_BUILDID: $(Build.BuildId)
              BUILD_SOURCEBRANCHNAME: $(Build.SourceBranchName)
              BUILD_SOURCEVERSION: $(Build.SourceVersion)

          - task: PublishPipelineArtifact@1
            inputs:
              targetPath: dist
              artifact: $(artifactName)
              publishLocation: pipeline
            displayName: Publish demo artifact (download from pipeline run)

          - script: |
              echo "Artifact published: $(artifactName)"
              echo "Download: Azure DevOps → this run → Related → Published artifacts → $(artifactName)"
              echo "See docs/ARTIFACT_DISTRIBUTION.md"
            displayName: Print download instructions
