name: Deploy

on:
  workflow_call:
    inputs:
      framework:
        description: ".net framework"
        type: string
        required: true
      branch:
        description: "Git branch used for this build"
        type: string
        required: true
      major_version:
        description: "Sonarr major version"
        type: string
        required: true
      version:
        description: "Sonarr version"
        type: string
        required: true
    secrets:
      SERVICES_API_KEY:
        required: true

jobs:
  package:
    strategy:
      matrix:
        include:
          - runtime: freebsd-x64
            os: ubuntu-latest
          - runtime: linux-arm
            os: ubuntu-latest
          - runtime: linux-arm64
            os: ubuntu-latest
          - runtime: linux-musl-arm64
            os: ubuntu-latest
          - runtime: linux-musl-x64
            os: ubuntu-latest
          - runtime: linux-x64
            os: ubuntu-latest
          - runtime: osx-arm64
            os: ubuntu-latest
          - runtime: osx-x64
            os: ubuntu-latest
          - runtime: win-x64
            os: windows-latest
          - runtime: win-x86
            os: windows-latest

    runs-on: ${{ matrix.os }}
    steps:
      - name: Check out
        uses: actions/checkout@v4

      - name: Package
        uses: ./.github/actions/package
        with:
          framework: ${{ inputs.framework }}
          runtime: ${{ matrix.runtime }}
          artifact: build-${{ matrix.runtime }}
          branch: ${{ inputs.branch }}
          major_version: ${{ inputs.major_version }}
          version: ${{ inputs.version }}

  release:
    needs: package
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Check out
        uses: actions/checkout@v4

      - name: Download release artifacts
        uses: actions/download-artifact@v4
        with:
          path: _artifacts
          pattern: release-*
          merge-multiple: true

      - name: Get Previous Release
        id: previous-release
        uses: cardinalby/git-get-release-action@v1
        env:
          GITHUB_TOKEN: ${{ github.token }}
        with:
          latest: true
          prerelease: ${{ inputs.branch != 'main' }}

      - name: Generate Release Notes
        id: generate-release-notes
        uses: actions/github-script@v7
        with:
          github-token: ${{ github.token }}
          result-encoding: string
          script: |
            const { data } = await github.rest.repos.generateReleaseNotes({
              owner: context.repo.owner,
              repo: context.repo.repo,
              tag_name: 'v${{ inputs.version }}',
              target_commitish: '${{ github.sha }}',
              previous_tag_name: '${{ steps.previous-release.outputs.tag_name }}',
            })
            return data.body

      - name: Create release
        uses: ncipollo/release-action@v1
        with:
          artifacts: _artifacts/Sonarr.*
          commit: ${{ github.sha }}
          generateReleaseNotes: false
          body: ${{ steps.generate-release-notes.outputs.result }}
          name: ${{ inputs.version }}
          prerelease: ${{ inputs.branch != 'main' }}
          skipIfReleaseExists: true
          tag: v${{ inputs.version }}

      - name: Publish to Services
        shell: bash
        working-directory: _artifacts
        run: |
          branch=${{ inputs.branch }}
          version=${{ inputs.version }}
          lastCommit=${{ github.sha }}

          hashes="["

          addHash() {
            path=$1
            os=$2
            arch=$3
            type=$4

            local hash=$(sha256sum *.$version.$path | awk '{ print $1; }')
            echo "{ \""Os\"": \""$os\"", \""Arch\"": \""$arch\"", \""Type\"": \""$type\"", \""Hash\"": \""$hash\"" }"
          }

          hashes="$hashes $(addHash "linux-arm.tar.gz" "linux" "arm" "archive")"
          hashes="$hashes, $(addHash "linux-arm64.tar.gz" "linux" "arm64" "archive")"
          hashes="$hashes, $(addHash "linux-x64.tar.gz" "linux" "x64" "archive")"
          # hashes="$hashes, $(addHash "linux-x86.tar.gz" "linux" "x86" "archive")"

          # hashes="$hashes, $(addHash "linux-musl-arm.tar.gz" "linuxmusl" "arm" "archive")"
          hashes="$hashes, $(addHash "linux-musl-arm64.tar.gz" "linuxmusl" "arm64" "archive")"
          hashes="$hashes, $(addHash "linux-musl-x64.tar.gz" "linuxmusl" "x64" "archive")"

          hashes="$hashes, $(addHash "osx-arm64.tar.gz" "osx" "arm64" "archive")"
          hashes="$hashes, $(addHash "osx-x64.tar.gz" "osx" "x64" "archive")"

          hashes="$hashes, $(addHash "osx-arm64-app.zip" "osx" "arm64" "installer")"
          hashes="$hashes, $(addHash "osx-x64-app.zip" "osx" "x64" "installer")"

          hashes="$hashes, $(addHash "win-x64.zip" "windows" "x64" "archive")"
          hashes="$hashes, $(addHash "win-x86.zip" "windows" "x86" "archive")"

          hashes="$hashes, $(addHash "win-x64-installer.exe" "windows" "x64" "installer")"
          hashes="$hashes, $(addHash "win-x86-installer.exe" "windows" "x86" "installer")"

          hashes="$hashes, $(addHash "freebsd-x64.tar.gz" "freebsd" "x64" "archive")"

          hashes="$hashes ]"

          json="{\""branch\"":\""$branch\"", \""version\"":\""$version\"", \""lastCommit\"":\""$lastCommit\"", \""hashes\"":$hashes, \""gitHubRelease\"":true}"
          url="https://services.sonarr.tv/v1/update"

          echo "Publishing update $version ($branch) to: $url"
          echo "$json"

          curl -H "Content-Type: application/json" -H "X-Api-Key: ${{ secrets.SERVICES_API_KEY }}" -X POST -d "$json" --fail-with-body $url
