name: Release

on:
  push:
    tags:
      - 'v*'

env:
  GITEA_URL: https://gitea.anthonybouteiller.ovh
  GITEA_REPO: blomios/TougliGui

jobs:
  # ── Crée la release Gitea à partir du tag ────────────────────────────────
  create-release:
    runs-on: [self-hosted, linux]
    outputs:
      release_id: ${{ steps.create.outputs.release_id }}
      version: ${{ steps.version.outputs.version }}
    steps:
      - name: Extract version
        id: version
        run: echo "version=${GITHUB_REF_NAME#v}" >> $GITHUB_OUTPUT

      - name: Create Gitea release
        id: create
        run: |
          RESPONSE=$(curl -s -X POST \
            "$GITEA_URL/api/v1/repos/$GITEA_REPO/releases" \
            -H "Authorization: token ${{ secrets.RELEASE_TOKEN }}" \
            -H "Content-Type: application/json" \
            -d "{
              \"tag_name\": \"$GITHUB_REF_NAME\",
              \"name\": \"TougliGui $GITHUB_REF_NAME\",
              \"body\": \"Release $GITHUB_REF_NAME\",
              \"draft\": false,
              \"prerelease\": false
            }")
          echo "release_id=$(echo $RESPONSE | python3 -c 'import json,sys; print(json.load(sys.stdin)[\"id\"])')" >> $GITHUB_OUTPUT

  # ── Build Linux ───────────────────────────────────────────────────────────
  build-linux:
    needs: create-release
    runs-on: [self-hosted, linux]
    steps:
      - uses: actions/checkout@v4

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: '22'

      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Install system deps
        run: |
          sudo apt-get update
          sudo apt-get install -y \
            libwebkit2gtk-4.1-dev libgtk-3-dev libayatana-appindicator3-dev \
            librsvg2-dev patchelf

      - name: Install npm deps
        run: npm ci

      - name: Build
        run: npm run tauri build

      - name: Upload artifacts
        run: |
          VERSION="${{ needs.create-release.outputs.version }}"
          RELEASE_ID="${{ needs.create-release.outputs.release_id }}"

          upload() {
            FILE="$1"
            NAME=$(basename "$FILE")
            curl -s -X POST \
              "$GITEA_URL/api/v1/repos/$GITEA_REPO/releases/$RELEASE_ID/assets?name=$NAME" \
              -H "Authorization: token ${{ secrets.RELEASE_TOKEN }}" \
              -F "attachment=@$FILE"
          }

          find src-tauri/target/release/bundle/deb      -name "*.deb"      | while read f; do upload "$f"; done
          find src-tauri/target/release/bundle/appimage -name "*.AppImage" | while read f; do upload "$f"; done
          find src-tauri/target/release/bundle/rpm      -name "*.rpm"      | while read f; do upload "$f"; done

  # ── Build Windows ─────────────────────────────────────────────────────────
  build-windows:
    needs: create-release
    runs-on: [self-hosted, windows]
    steps:
      - uses: actions/checkout@v4

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: '22'

      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable

      - name: Install npm deps
        run: npm ci

      - name: Build
        run: npm run tauri build

      - name: Upload artifacts
        shell: bash
        run: |
          RELEASE_ID="${{ needs.create-release.outputs.release_id }}"

          upload() {
            FILE="$1"
            NAME=$(basename "$FILE")
            curl -s -X POST \
              "$GITEA_URL/api/v1/repos/$GITEA_REPO/releases/$RELEASE_ID/assets?name=$NAME" \
              -H "Authorization: token ${{ secrets.RELEASE_TOKEN }}" \
              -F "attachment=@$FILE"
          }

          find src-tauri/target/release/bundle/nsis -name "*.exe" | while read f; do upload "$f"; done
          find src-tauri/target/release/bundle/msi  -name "*.msi" | while read f; do upload "$f"; done

  # ── Build macOS ───────────────────────────────────────────────────────────
  build-macos:
    needs: create-release
    runs-on: [self-hosted, macos]
    steps:
      - uses: actions/checkout@v4

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: '22'

      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: aarch64-apple-darwin x86_64-apple-darwin

      - name: Install npm deps
        run: npm ci

      - name: Build (universal)
        run: npm run tauri build -- --target universal-apple-darwin

      - name: Upload artifacts
        run: |
          RELEASE_ID="${{ needs.create-release.outputs.release_id }}"

          upload() {
            FILE="$1"
            NAME=$(basename "$FILE")
            curl -s -X POST \
              "$GITEA_URL/api/v1/repos/$GITEA_REPO/releases/$RELEASE_ID/assets?name=$NAME" \
              -H "Authorization: token ${{ secrets.RELEASE_TOKEN }}" \
              -F "attachment=@$FILE"
          }

          find src-tauri/target/universal-apple-darwin/release/bundle/dmg    -name "*.dmg"        | while read f; do upload "$f"; done
          find src-tauri/target/universal-apple-darwin/release/bundle/macos  -name "*.app.tar.gz" | while read f; do upload "$f"; done
