name: NuGet Publish

on:
  push:
    branches:
      - "**"

jobs:
  build-and-pack:
    runs-on: ubuntu-latest
    container:
      image: mcr.microsoft.com/dotnet/sdk:10.0
    env:
      GITHUB_TOKEN: ${{ github.token }}
    steps:
      - name: Checkout Repository
        run: |
          set -eu
          REPO_URL="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git"
          if [ -n "${GITHUB_TOKEN:-}" ]; then
            HOST="${GITHUB_SERVER_URL#https://}"
            REPO_URL="https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@${HOST}/${GITHUB_REPOSITORY}.git"
          fi

          git init .
          git config --global --add safe.directory "$PWD"
          git remote add origin "$REPO_URL"
          git fetch --depth=1 origin "${GITHUB_SHA}"
          git checkout --detach FETCH_HEAD

      - name: Restore
        run: dotnet restore CBDD.slnx

      - name: Build
        run: dotnet build CBDD.slnx -c Release --no-restore

      - name: Pack NuGet Packages
        run: |
          mkdir -p nupkgs
          dotnet pack src/CBDD/ZB.MOM.WW.CBDD.csproj -c Release -o nupkgs --no-build
          dotnet pack src/CBDD.Bson/ZB.MOM.WW.CBDD.Bson.csproj -c Release -o nupkgs --no-build
          dotnet pack src/CBDD.Core/ZB.MOM.WW.CBDD.Core.csproj -c Release -o nupkgs --no-build
          dotnet pack src/CBDD.SourceGenerators/ZB.MOM.WW.CBDD.SourceGenerators.csproj -c Release -o nupkgs --no-build

  publish-to-gitea:
    needs: build-and-pack
    runs-on: ubuntu-latest
    if: ${{ github.ref == 'refs/heads/main' }}
    container:
      image: mcr.microsoft.com/dotnet/sdk:10.0
    env:
      GITHUB_TOKEN: ${{ github.token }}
      GITEA_NUGET_USERNAME: ${{ secrets.GITEA_NUGET_USERNAME }}
      GITEA_NUGET_TOKEN: ${{ secrets.GITEA_NUGET_TOKEN }}
    steps:
      - name: Checkout Repository
        run: |
          set -eu
          REPO_URL="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git"
          if [ -n "${GITHUB_TOKEN:-}" ]; then
            HOST="${GITHUB_SERVER_URL#https://}"
            REPO_URL="https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@${HOST}/${GITHUB_REPOSITORY}.git"
          fi

          git init .
          git config --global --add safe.directory "$PWD"
          git remote add origin "$REPO_URL"
          git fetch --depth=1 origin "${GITHUB_SHA}"
          git checkout --detach FETCH_HEAD

      - name: Restore
        run: dotnet restore CBDD.slnx

      - name: Build
        run: dotnet build CBDD.slnx -c Release --no-restore

      - name: Pack NuGet Packages
        run: |
          mkdir -p nupkgs
          dotnet pack src/CBDD/ZB.MOM.WW.CBDD.csproj -c Release -o nupkgs --no-build
          dotnet pack src/CBDD.Bson/ZB.MOM.WW.CBDD.Bson.csproj -c Release -o nupkgs --no-build
          dotnet pack src/CBDD.Core/ZB.MOM.WW.CBDD.Core.csproj -c Release -o nupkgs --no-build
          dotnet pack src/CBDD.SourceGenerators/ZB.MOM.WW.CBDD.SourceGenerators.csproj -c Release -o nupkgs --no-build

      - name: Configure Gitea NuGet Source
        run: |
          set -eu
          USERNAME="${GITEA_NUGET_USERNAME:-${GITHUB_ACTOR}}"
          TOKEN="${GITEA_NUGET_TOKEN:-${GITHUB_TOKEN:-}}"

          if [ -z "$TOKEN" ]; then
            echo "No token available for NuGet publish"
            exit 1
          fi

          dotnet nuget remove source gitea >/dev/null 2>&1 || true
          dotnet nuget add source \
            "${GITHUB_SERVER_URL}/api/packages/${GITHUB_REPOSITORY_OWNER}/nuget/index.json" \
            --name gitea \
            --username "$USERNAME" \
            --password "$TOKEN" \
            --store-password-in-clear-text

      - name: Publish Packages
        run: |
          set -eu
          for pkg in nupkgs/*.nupkg; do
            [ -f "$pkg" ] || continue
            case "$pkg" in
              *.snupkg) continue ;;
            esac
            dotnet nuget push "$pkg" --source gitea --skip-duplicate
          done
