name: Build Windows

on:
  workflow_dispatch:
    inputs:
      version:
        description: "Version to build (e.g. 0.4.0)"
        required: true

permissions:
  contents: write

jobs:
  frontend:
    name: Build frontend
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - uses: pnpm/action-setup@v4
        with:
          version: latest

      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: pnpm

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Build
        run: pnpm build

      - name: Upload dist
        uses: actions/upload-artifact@v4
        with:
          name: frontend-dist-windows
          path: dist/
          retention-days: 1

  tauri:
    name: Tauri (Windows x64)
    needs: frontend
    runs-on: windows-latest

    steps:
      - uses: actions/checkout@v4

      - name: Download frontend dist
        uses: actions/download-artifact@v4
        with:
          name: frontend-dist-windows
          path: dist/

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: x86_64-pc-windows-msvc

      - name: Rust cache
        uses: Swatinem/rust-cache@v2
        with:
          workspaces: src-tauri

      - uses: pnpm/action-setup@v4
        with:
          version: latest

      - uses: actions/setup-node@v4
        with:
          node-version: 22
          cache: pnpm

      - name: Install JS dependencies
        run: pnpm install --frozen-lockfile

      - name: Download Suwayomi (Windows x64)
        shell: bash
        run: |
          curl -fsSL \
            "https://github.com/Suwayomi/Suwayomi-Server/releases/download/v2.1.1867/Suwayomi-Server-v2.1.1867-windows-x64.zip" \
            -o suwayomi-windows.zip
          echo "ab6687d278e0dd0984f67abbc853511a7e764f84b126a35d09bfd9b0307321ff  suwayomi-windows.zip" | sha256sum -c -
          unzip -q suwayomi-windows.zip -d suwayomi-raw

      - name: Extract Suwayomi bundle
        shell: bash
        run: |
          mkdir -p suwayomi-extracted
          TOP_DIRS=$(find suwayomi-raw -mindepth 1 -maxdepth 1 -type d | wc -l)
          TOP_FILES=$(find suwayomi-raw -mindepth 1 -maxdepth 1 -type f | wc -l)
          if [ "$TOP_DIRS" -eq 1 ] && [ "$TOP_FILES" -eq 0 ]; then
            INNER=$(find suwayomi-raw -mindepth 1 -maxdepth 1 -type d | head -1)
            cp -r "$INNER"/. suwayomi-extracted/
          else
            cp -r suwayomi-raw/. suwayomi-extracted/
          fi

      - name: Stage Suwayomi bundle
        shell: bash
        run: |
          mkdir -p src-tauri/binaries
          JAVA=$(find suwayomi-extracted -path "*/jre/bin/java.exe" | head -1)
          JAR=$(find suwayomi-extracted -name "Suwayomi-Server.jar" | head -1)
          if [ -z "$JAVA" ]; then
            echo "ERROR: jre/bin/java.exe not found"
            find suwayomi-extracted -type f | head -50
            exit 1
          fi
          if [ -z "$JAR" ]; then
            echo "ERROR: Suwayomi-Server.jar not found"
            find suwayomi-extracted -type f | head -50
            exit 1
          fi
          cp -r suwayomi-extracted src-tauri/binaries/suwayomi-bundle

      - name: Validate staging
        shell: bash
        run: |
          find src-tauri/binaries/suwayomi-bundle -path "*/jre/bin/java.exe" \
            | grep -q . || (echo "ERROR: jre/bin/java.exe missing" && exit 1)
          find src-tauri/binaries/suwayomi-bundle -name "Suwayomi-Server.jar" \
            | grep -q . || (echo "ERROR: Suwayomi-Server.jar missing" && exit 1)
          echo "Staging OK"

      - name: Patch tauri.conf.json for CI
        shell: bash
        run: |
          sed -i 's/"beforeBuildCommand": "pnpm build"/"beforeBuildCommand": ""/' src-tauri/tauri.conf.json

      - name: Delete existing draft release if present
        shell: bash
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          RELEASE_ID=$(curl -s -H "Authorization: Bearer $GITHUB_TOKEN"             "https://api.github.com/repos/Youwes09/Moku/releases"             | jq -r '.[] | select(.tag_name == "v${{ github.event.inputs.version }}" and .draft == true) | .id')
          if [ -n "$RELEASE_ID" ]; then
            echo "Deleting existing draft release $RELEASE_ID"
            curl -s -X DELETE -H "Authorization: Bearer $GITHUB_TOKEN"               "https://api.github.com/repos/Youwes09/Moku/releases/$RELEASE_ID"
            # Also delete the tag so tauri-action can recreate it
            curl -s -X DELETE -H "Authorization: Bearer $GITHUB_TOKEN"               "https://api.github.com/repos/Youwes09/Moku/git/refs/tags/v${{ github.event.inputs.version }}"
            echo "Deleted draft release and tag"
          else
            echo "No existing draft release found"
          fi

      - name: Build Tauri app + create draft release
        uses: tauri-apps/tauri-action@v0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }}
          TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }}
        with:
          tagName: v${{ github.event.inputs.version }}
          releaseName: Moku v${{ github.event.inputs.version }}
          releaseBody: |
            Windows installer for Moku v${{ github.event.inputs.version }}.
            Download the `.exe` file below to install or update.
          releaseDraft: true
          prerelease: false
          args: --target x86_64-pc-windows-msvc --config src-tauri/tauri.windows.conf.json
