name: PR WinUI Build

on:
  pull_request:
    types:
      - opened
      - synchronize
      - reopened
      - ready_for_review

permissions:
  contents: read
  packages: read

jobs:
  build-winui:
    name: Build project (${{ matrix.platform }})
    if: github.event.pull_request.draft == false
    runs-on: windows-latest
    continue-on-error: ${{ contains(fromJSON('["OWNER","MEMBER","COLLABORATOR"]'), github.event.pull_request.author_association) }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - platform: x86
            rid: win-x86
          - platform: x64
            rid: win-x64
          - platform: ARM64
            rid: win-arm64

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

      - name: Setup .NET SDK
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: 10.0.x
          source-url: https://nuget.pkg.github.com/bkaankose/index.json
        env:
          NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Restore WinUI project dependencies
        run: dotnet restore Wino.Mail.WinUI/Wino.Mail.WinUI.csproj --configfile nuget.config -p:Platform=${{ matrix.platform }} -p:RuntimeIdentifier=${{ matrix.rid }}

      - name: Build WinUI project
        run: dotnet build Wino.Mail.WinUI/Wino.Mail.WinUI.csproj --configuration Release --no-restore -p:Platform=${{ matrix.platform }} -p:RuntimeIdentifier=${{ matrix.rid }} -p:GenerateAppxPackageOnBuild=false -p:AppxPackageSigningEnabled=false

  core-tests:
    name: Run Core tests
    if: github.event.pull_request.draft == false
    runs-on: windows-latest
    continue-on-error: ${{ contains(fromJSON('["OWNER","MEMBER","COLLABORATOR"]'), github.event.pull_request.author_association) }}

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

      - name: Setup .NET SDK
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: 10.0.x
          source-url: https://nuget.pkg.github.com/bkaankose/index.json
        env:
          NUGET_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Restore Core test projects
        shell: pwsh
        run: |
          $coreTests = Get-ChildItem -Path . -Recurse -Filter "*Core*.Tests.csproj" | ForEach-Object { $_.FullName }
          if (-not $coreTests) {
            throw "No Core test projects were found."
          }

          foreach ($project in $coreTests) {
            dotnet restore $project --configfile nuget.config
          }

      - name: Run Core test projects
        shell: pwsh
        run: |
          New-Item -ItemType Directory -Path TestResults -Force | Out-Null
          $coreTests = Get-ChildItem -Path . -Recurse -Filter "*Core*.Tests.csproj"
          if (-not $coreTests) {
            throw "No Core test projects were found."
          }

          foreach ($project in $coreTests) {
            $name = $project.BaseName
            dotnet test $project.FullName --configuration Release --no-restore --verbosity normal --logger "trx;LogFileName=$name.trx" --results-directory TestResults
          }

      - name: Upload Core test result artifacts
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: core-test-results
          path: TestResults/*.trx
          if-no-files-found: warn

      - name: Publish Core test report
        if: always()
        uses: EnricoMi/publish-unit-test-result-action/windows@v2
        with:
          trx_files: TestResults/*.trx
          check_name: Core test results

  enforce-for-non-maintainers:
    name: Enforce required checks (non-maintainers)
    if: github.event.pull_request.draft == false && !contains(fromJSON('["OWNER","MEMBER","COLLABORATOR"]'), github.event.pull_request.author_association)
    runs-on: ubuntu-latest
    needs:
      - build-winui
      - core-tests

    steps:
      - name: Fail when build or tests fail for non-maintainers
        if: needs.build-winui.result != 'success' || needs.core-tests.result != 'success'
        run: |
          echo "WinUI build and Core tests must pass for non-maintainer pull requests."
          exit 1

      - name: Confirm build and test success for non-maintainers
        run: echo "WinUI build and Core tests passed."
