# .NET MAUI Linux Platform CI/CD Pipeline
name: CI

on:
  push:
    branches: [ main, develop ]
  pull_request:
    branches: [ main ]
  workflow_dispatch:

env:
  DOTNET_VERSION: '9.0.x'
  DOTNET_NOLOGO: true
  DOTNET_CLI_TELEMETRY_OPTOUT: true

jobs:
  build:
    name: Build and Test
    runs-on: ubuntu-latest

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

    - name: Setup .NET
      uses: actions/setup-dotnet@v4
      with:
        dotnet-version: ${{ env.DOTNET_VERSION }}

    - name: Install Linux dependencies
      run: |
        sudo apt-get update
        sudo apt-get install -y libx11-dev libxrandr-dev libxcursor-dev libxi-dev libgl1-mesa-dev

    - name: Restore dependencies
      run: dotnet restore

    - name: Build
      run: dotnet build --configuration Release --no-restore

    - name: Run tests
      run: dotnet test --configuration Release --no-build --verbosity normal --logger "trx;LogFileName=test-results.trx"

    - name: Upload test results
      uses: actions/upload-artifact@v6
      if: always()
      with:
        name: test-results
        path: '**/TestResults/*.trx'

    - name: Upload build artifacts
      uses: actions/upload-artifact@v6
      with:
        name: build-artifacts
        path: |
          **/bin/Release/**/*.dll
          **/bin/Release/**/*.xml

  pack:
    name: Create NuGet Package
    runs-on: ubuntu-latest
    needs: build
    if: github.ref == 'refs/heads/main' || github.event_name == 'workflow_dispatch'

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

    - name: Setup .NET
      uses: actions/setup-dotnet@v4
      with:
        dotnet-version: ${{ env.DOTNET_VERSION }}

    - name: Install Linux dependencies
      run: |
        sudo apt-get update
        sudo apt-get install -y libx11-dev libxrandr-dev libxcursor-dev libxi-dev libgl1-mesa-dev

    - name: Restore dependencies
      run: dotnet restore

    - name: Pack NuGet package
      run: dotnet pack --configuration Release --no-restore -o ./nupkg

    - name: Upload NuGet package
      uses: actions/upload-artifact@v6
      with:
        name: nuget-package
        path: ./nupkg/*.nupkg

  code-quality:
    name: Code Quality
    runs-on: ubuntu-latest

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

    - name: Setup .NET
      uses: actions/setup-dotnet@v4
      with:
        dotnet-version: ${{ env.DOTNET_VERSION }}

    - name: Install Linux dependencies
      run: |
        sudo apt-get update
        sudo apt-get install -y libx11-dev libxrandr-dev libxcursor-dev libxi-dev libgl1-mesa-dev

    - name: Restore dependencies
      run: dotnet restore

    - name: Build with warnings as errors
      run: dotnet build --configuration Release --no-restore /warnaserror
      continue-on-error: true

    - name: Format check
      run: dotnet format --verify-no-changes --verbosity diagnostic
      continue-on-error: true
