name: FModel QA Builder

on:
  push:
    branches: [ dev ]

jobs:
  build:
    runs-on: windows-latest

    steps:
      - name: GIT Checkout
        uses: actions/checkout@v6
        with:
          submodules: 'recursive'

      - name: .NET 8 Setup
        uses: actions/setup-dotnet@v5
        with:
          dotnet-version: '8.0.x'

      - name: .NET Restore
        run: dotnet restore "./FModel/FModel.slnx"

      - name: .NET Publish
        run: dotnet publish "./FModel/FModel.csproj" -c Release --no-restore --no-self-contained -r win-x64 -f net8.0-windows -o "./FModel/bin/Publish/" -p:PublishReadyToRun=false -p:PublishSingleFile=true -p:DebugType=None -p:GenerateDocumentationFile=false -p:DebugSymbols=false

      - name: ZIP File
        uses: thedoctor0/zip-release@0.7.6
        with:
          type: zip
          filename: ${{ github.sha }}.zip # will end up in working directory not the Publish folder
          path: ./FModel/bin/Publish/FModel.exe

      - name: Edit QA Artifact
        uses: ncipollo/release-action@v1.20.0
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          name: 'FModel QA Testing'
          body: 'Dev builds'
          tag: 'qa'
          artifacts: ${{ github.sha }}.zip
          prerelease: true
          allowUpdates: true

      - name: Get Version
        id: package_version
        uses: kzrnm/get-net-sdk-project-versions-action@v2
        with:
          proj-path: ./FModel/FModel.csproj

      - name: FModel Auth
        id: fmodel_auth
        uses: fjogeleit/http-request-action@v1.16.6
        with:
          url: "https://api.fmodel.app/v1/oauth/token"
          data: '{"username": "${{ secrets.API_USERNAME }}", "password": "${{ secrets.API_PASSWORD }}"}'

      - name: FModel Deploy Build
        uses: fjogeleit/http-request-action@v1.16.6
        with:
          url: "https://api.fmodel.app/v1/infos/${{ secrets.QA_ID }}"
          method: "PATCH"
          bearerToken: ${{ fromJson(steps.fmodel_auth.outputs.response).accessToken }}
          data: '{"version": "${{ steps.package_version.outputs.version }}-dev+${{ github.sha }}", "downloadUrl": "https://github.com/4sval/FModel/releases/download/qa/${{ github.sha }}.zip"}'
