on:
  push:
    tags:
      - "v*.*.*"
  workflow_dispatch:

name: Upload Release Assets

permissions:
  contents: write
  pull-requests: write

jobs:
  build:
    uses: ./.github/workflows/build.yml
    secrets: inherit

  post-build:
    name: Release
    needs: build
    runs-on: ubuntu-latest

    steps:
      - name: Download build artifact
        uses: actions/download-artifact@v4
        with:
          name: mdfried-static # mdfried

      - name: Download build artifact (windows)
        uses: actions/download-artifact@v4
        with:
          name: mdfried-windows # mdfried.exe

      - name: Download build artifact (macOS x86_64)
        uses: actions/download-artifact@v4
        with:
          name: mdfried_macos_x86_64
          path: macos-x86_64-build

      - name: Download build artifact (macOS aarch64)
        uses: actions/download-artifact@v4
        with:
          name: mdfried_macos_aarch64
          path: macos-aarch64-build

      - name: Rename macOS binaries
        run: |
          mv macos-x86_64-build/mdfried ./mdfried_macos_x86_64
          mv macos-aarch64-build/mdfried ./mdfried_macos_aarch64

      - name: Download .deb artifact
        uses: actions/download-artifact@v4
        with:
          name: mdfried-deb

      - name: Verify build outputs (removeme)
        run: |
          echo "Files in build-output directory:"
          ls

      - name: Create Github Release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.PAT }}
        with:
          tag_name: ${{ github.ref }}
          release_name: Release ${{ github.ref }}
          draft: false
          prerelease: false

      - name: Add release assets
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.PAT }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./mdfried
          asset_name: mdfried
          asset_content_type: application/octet-stream

      - name: Add release assets
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.PAT }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./mdfried.exe
          asset_name: mdfried.exe
          asset_content_type: application/octet-stream

      - name: Add release assets (macOS x86_64)
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.PAT }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./mdfried_macos_x86_64
          asset_name: mdfried_macos_x86_64
          asset_content_type: application/octet-stream

      - name: Add release assets (macOS aarch64)
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.PAT }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./mdfried_macos_aarch64
          asset_name: mdfried_macos_aarch64
          asset_content_type: application/octet-stream

      - name: Find .deb file
        id: find_deb
        run: |
          DEB_FILE=$(ls *.deb)
          echo "deb_file=$DEB_FILE" >> $GITHUB_OUTPUT
          echo "Found .deb file: $DEB_FILE"

      - name: Add .deb release asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.PAT }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ./${{ steps.find_deb.outputs.deb_file }}
          asset_name: ${{ steps.find_deb.outputs.deb_file }}
          asset_content_type: application/vnd.debian.binary-package
