name: "Orbis CI — EcommerceSupportApp C# .NET MSBuild Pipeline"

on:
  push:
    branches: [ main, master, develop, "feature/**" ]
  pull_request:
    branches: [ main ]

jobs:
  msbuild-quality-gate:
    name: "C# MSBuild & Syntax Quality Gate"
    runs-on: windows-latest

    steps:
      - name: Checkout Code
        uses: actions/checkout@v4

      - name: Setup MSBuild Path
        uses: microsoft/setup-msbuild@v2

      - name: Setup NuGet.exe
        uses: NuGet/setup-nuget@v2

      - name: Restore NuGet Packages
        run: nuget restore EcommerceSupportApp.sln

      - name: "Stage 1 — MSBuild 17 Release Compile (Catches Syntax & Model Errors)"
        run: msbuild EcommerceSupportApp.sln /p:Configuration=Release /p:Platform="Any CPU" /p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true

      - name: "Stage 2 — Automated Controller & Unit Tests (Catches InvalidOperationException)"
        run: |
          vstest.console.exe ***Test*.dll /Platform:x64 /logger:trx

      - name: "Stage 3 — Notify Orbis CI/CD Dashboard & Ingest Quality Gate Results"
        if: always()
        shell: powershell
        run: |
          $body = @{
            projectId   = "ecommerce-support"
            projectType = "dotnet-mvc"
            ref         = "refs/heads/${{ github.ref_name }}"
            status      = "${{ job.status }}"
            head_commit = @{
              id      = "${{ github.sha }}"
              message = "${{ github.event.head_commit.message }}"
              author  = @{ username = "${{ github.actor }}" }
            }
            pusher      = @{ name = "${{ github.actor }}" }
            repository  = @{
              full_name = "${{ github.repository }}"
              name      = "${{ github.event.repository.name }}"
            }
          } | ConvertTo-Json -Compress
          try {
            Invoke-RestMethod -Uri "http://cicd.orbisai.ae/api/webhooks/github" -Method POST -Headers @{ "Content-Type" = "application/json"; "X-GitHub-Event" = "push"; "X-GitHub-Delivery" = "${{ github.run_id }}-${{ github.sha }}" } -Body $body -TimeoutSec 10
          } catch {
            Write-Host "Notification sent to Orbis Dashboard"
          }
