name: Release

on:
  push:
    tags:
      - "v*"

jobs:
  release:
    runs-on: ubuntu-latest
    container:
      image: ubuntu:22.04
    env:
      DEBIAN_FRONTEND: noninteractive
    steps:
      - uses: actions/checkout@v4

      - name: Install system dependencies
        run: |
          apt-get update
          apt-get install -y \
            build-essential curl wget pkg-config jq file \
            libgtk-3-dev libwebkit2gtk-4.1-dev libappindicator3-dev librsvg2-dev \
            libssl-dev git ca-certificates

      - name: Install Node.js 22
        run: |
          curl -fsSL https://deb.nodesource.com/setup_22.x | bash -
          apt-get install -y nodejs

      - name: Install Rust toolchain
        run: |
          curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain stable
          echo "$HOME/.cargo/bin" >> $GITHUB_PATH

      - name: Install frontend dependencies
        run: npm ci

      - name: Build Tauri app
        run: |
          . "$HOME/.cargo/env"
          npm run tauri build

      - name: Create release and upload assets
        env:
          GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
        run: |
          TAG_NAME="${GITHUB_REF_NAME}"
          REPO="${GITHUB_REPOSITORY}"
          SERVER_URL="${GITHUB_SERVER_URL}"
          API_URL="${SERVER_URL}/api/v1"

          # Create release
          RELEASE_RESPONSE=$(curl -s -X POST \
            "${API_URL}/repos/${REPO}/releases" \
            -H "Authorization: token ${GITEA_TOKEN}" \
            -H "Content-Type: application/json" \
            -d "{
              \"tag_name\": \"${TAG_NAME}\",
              \"name\": \"Tusk ${TAG_NAME}\",
              \"body\": \"See the assets below to download Tusk for your platform.\",
              \"draft\": true,
              \"prerelease\": false
            }")

          RELEASE_ID=$(echo "${RELEASE_RESPONSE}" | jq -r '.id')

          if [ "${RELEASE_ID}" = "null" ] || [ -z "${RELEASE_ID}" ]; then
            echo "Failed to create release:"
            echo "${RELEASE_RESPONSE}"
            exit 1
          fi

          echo "Created release with ID: ${RELEASE_ID}"

          # Upload assets
          upload_asset() {
            local file="$1"
            local filename=$(basename "${file}")
            echo "Uploading ${filename}..."
            curl -s -X POST \
              "${API_URL}/repos/${REPO}/releases/${RELEASE_ID}/assets?name=${filename}" \
              -H "Authorization: token ${GITEA_TOKEN}" \
              -H "Content-Type: application/octet-stream" \
              --data-binary "@${file}"
          }

          for pattern in \
            "src-tauri/target/release/bundle/deb/*.deb" \
            "src-tauri/target/release/bundle/rpm/*.rpm" \
            "src-tauri/target/release/bundle/appimage/*.AppImage"; do
            for file in ${pattern}; do
              [ -f "${file}" ] && upload_asset "${file}"
            done
          done

          echo "Release created successfully"
