name: SharedLibraryCore NuGet

on:
  push:
    branches: [ develop, release/pre, master ]
    paths:
      - SharedLibraryCore/**
      - Data/**
      - .github/workflows/shared_library_nuget.yml
  pull_request:
    branches: [ develop ]
    paths:
      - SharedLibraryCore/**
      - Data/**

env:
  outputDirectory: ${{ github.workspace}}/nuget

jobs:
  update_revision_number:
    runs-on: ubuntu-latest

    outputs:
      revision_number: ${{ steps.revision.outputs.revision_number }}

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

      - name: Restore cache
        id: cache
        uses: actions/cache@v4
        with:
          path: cache_dir
          key: revision-number-nuget

      - name: Get current date
        id: date
        run: echo "current_date=$(date +'%Y-%m-%d')" >> $GITHUB_ENV

      - name: Check and update revision number
        id: revision
        run: |
          FILENAME=cache_dir/revision_number.txt
          DATEFILE=cache_dir/previous_date.txt

          mkdir -p cache_dir

          if [ -f "$DATEFILE" ]; then
            prev_date=$(cat "$DATEFILE")
            rev_number=$(cat "$FILENAME")
          else
            prev_date=""
            rev_number=0
          fi

          if [ "$current_date" = "$prev_date" ]; then
            rev_number=$((rev_number + 1))
          else
            rev_number=1
          fi

          echo "New revision number: $rev_number"
          echo $rev_number > "$FILENAME"
          echo $current_date > "$DATEFILE"
          echo "revision_number=$rev_number" >> $GITHUB_OUTPUT

      - name: Save cache
        uses: actions/cache@v4
        with:
          path: cache_dir
          key: revision-number-nuget
          
  make_version:
    runs-on: ubuntu-latest
    needs: [ update_revision_number ]
    
    outputs:
      build_num: ${{ steps.generate_build_number.outputs.build_num }}

    env:
      revisionNumber: ${{ needs.update_revision_number.outputs.revision_number }}
      
    steps:
      - name: Make build number
        id: generate_build_number
        run: |
          build_num=$(date +'%Y.%-m.%-d').${{ env.revisionNumber }}
          echo "build_num=$build_num" >> $GITHUB_OUTPUT
          echo "Build number is $build_num"
          
  build_pack:
    runs-on: ubuntu-latest
    needs: [ make_version ]
      
    env:
      buildNumber: ${{ needs.make_version.outputs.build_num }}
      packageTag: ${{ github.event_name == 'pull_request' && '-beta' || '-preview' }}
      buildConfiguration: Prerelease
      
    steps:
      - uses: actions/checkout@v4
        
      - name: Setup .NET
        uses: actions/setup-dotnet@v4
        with:
          dotnet-version: 8.0.x
          
      - name: Restore dependencies
        run: dotnet restore
        
      - name: Build data
        run: dotnet build **/Data.csproj -c ${{env.buildConfiguration}} /p:Version=${{ env.buildNumber }} --no-restore
        
      - name: Build SLC
        run: dotnet build **/SharedLibraryCore.csproj -c ${{env.buildConfiguration}} /p:Version=${{ env.buildNumber }} --no-restore
        
      - name: Pack SLC
        run: dotnet pack **/SharedLibraryCore.csproj -c ${{env.buildConfiguration}} -p:PackageVersion=${{ env.buildNumber }}${{ env.packageTag }} -o ${{ env.outputDirectory }} --no-restore

      - name: Publish nuget package artifact
        uses: actions/upload-artifact@v4
        with:
          name: SharedLibraryCore-${{ env.buildNumber }}
          path: ${{ env.outputDirectory }}/*.nupkg
  
  publish:
    runs-on: ubuntu-latest
    
    needs: [ make_version, build_pack ]
    environment: prerelease
    if: ${{ github.ref == 'refs/heads/master' || github.ref == 'refs/heads/release/pre' || github.ref == 'refs/heads/develop' }}
    
    steps:
      - name: Download artifact
        uses: actions/download-artifact@v4
        with:
          name: SharedLibraryCore-${{ needs.make_version.outputs.build_num }}
          path: ${{ env.outputDirectory }}

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

      - name: Publish NuGet package
        run: |
          for file in ${{ env.outputDirectory }}/*.nupkg; do
            dotnet nuget push "$file" --api-key "${{ secrets.NUGET_APIKEY }}" --source https://api.nuget.org/v3/index.json --skip-duplicate
          done
