name: Publish
on:
  workflow_dispatch:
  push:
    branches:
      - main
jobs:
  version:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          token: ${{ secrets.ACTIONS_PAT }}
      - name: Setup .NET 9
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: 9.x
      - name: Install versionize
        run: dotnet tool install --global Versionize
      - name: Setup git
        run: |
          git config --local user.email "65925598+StevanFreeborn@users.noreply.github.com"
          git config --local user.name "Stevan Freeborn"
      - name: Run versionize
        id: versionize
        run: versionize -i --exit-insignificant-commits --workingDir ./src/AnthropicClient --commit-suffix "[skip ci]"
        continue-on-error: true
      - name: Upload changelog
        if: steps.versionize.outcome == 'success'
        uses: actions/upload-artifact@v4
        with:
          name: change-log
          path: src/AnthropicClient/CHANGELOG.md
      - name: Push changes to GitHub
        if: steps.versionize.outcome == 'success'
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.ACTIONS_PAT }}
          branch: ${{ github.ref }}
          tags: true
    outputs:
      is_new_version: ${{ steps.versionize.outcome == 'success' }}
  publish:
    needs: version
    if: needs.version.outputs.is_new_version == 'true'
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          ref: ${{ github.ref }}
          token: ${{ secrets.ACTIONS_PAT }}
      - name: Setup .NET 9
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: 9.x
      - name: Get project version
        uses: kzrnm/get-net-sdk-project-versions-action@v1
        id: get-version
        with:
          proj-path: src/AnthropicClient/AnthropicClient.csproj
      - name: Download changlog
        uses: actions/download-artifact@v4
        with:
          name: change-log
          path: src/AnthropicClient
      - name: Publish NuGet package
        run: |
          dotnet pack src/AnthropicClient/AnthropicClient.csproj -o ./artifacts
          dotnet nuget push ./artifacts/*.nupkg --api-key ${{ secrets.NUGET_KEY }} --source https://api.nuget.org/v3/index.json
      - name: Create GitHub release
        uses: softprops/action-gh-release@v1
        with:
          token: ${{ secrets.ACTIONS_PAT }}
          name: AnthropicClient v${{ steps.get-version.outputs.version }}
          tag_name: v${{ steps.get-version.outputs.version }}
          body_path: src/AnthropicClient/CHANGELOG.md
  documentation:
    needs: version
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          ref: ${{ github.ref }}
          token: ${{ secrets.ACTIONS_PAT }}
      - name: Setup .NET 9
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: 9.x
      - name: Install Docfx
        run: dotnet tool install --global docfx
      - name: Get project version
        uses: kzrnm/get-net-sdk-project-versions-action@v1
        id: get-version
        with:
          proj-path: src/AnthropicClient/AnthropicClient.csproj
      - name: Build documentation
        run: |
          cp README.md docfx/index.md
          docfx metadata docfx/docfx.json
          docfx build docfx/docfx.json
      - name: Commit documentation
        run: |
          git config --local user.email "65925598+StevanFreeborn@users.noreply.github.com"
          git config --local user.name "Stevan Freeborn" 
          git add .
          git commit -m "docs: documentation for v${{ steps.get-version.outputs.version }} [skip ci]"
        continue-on-error: true
      - name: Push documentation
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.ACTIONS_PAT }}
          branch: ${{ github.ref }}
          tags: true
        continue-on-error: true