name: Build

on:
  push:
    branches:
      - main
      - develop
    tags-ignore:
      - '**'
  pull_request:
    branches:
      - main
      - develop
  workflow_dispatch:

jobs:
  build-desktop:
    strategy:
      fail-fast: false
      matrix:
        include:
          - platform: 'macos-latest'
            args: '--target aarch64-apple-darwin'
          - platform: 'macos-latest'
            args: '--target x86_64-apple-darwin'
          - platform: 'ubuntu-22.04'
            args: ''
          - platform: 'windows-latest'
            args: ''

    runs-on: ${{ matrix.platform }}
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Install pnpm
        uses: pnpm/action-setup@v4

      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.platform == 'macos-latest' && 'aarch64-apple-darwin,x86_64-apple-darwin' || '' }}

      - name: Install dependencies (Ubuntu)
        if: matrix.platform == 'ubuntu-22.04'
        run: |
          sudo apt-get update
          sudo apt-get install -y libwebkit2gtk-4.1-dev libappindicator3-dev librsvg2-dev patchelf libssl-dev

      - name: Get pnpm store directory
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV

      - name: Setup pnpm cache
        uses: actions/cache@v4
        with:
          path: ${{ env.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-

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

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

      - name: Build Tauri app
        uses: tauri-apps/tauri-action@v0
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          args: ${{ matrix.args }}

      - name: Upload artifacts (macOS)
        if: matrix.platform == 'macos-latest'
        uses: actions/upload-artifact@v4
        with:
          name: macos-${{ contains(matrix.args, 'aarch64') && 'aarch64' || 'x86_64' }}
          path: |
            src-tauri/target/*/release/bundle/dmg/*.dmg
            src-tauri/target/*/release/bundle/macos/*.app

      - name: Upload artifacts (Ubuntu)
        if: matrix.platform == 'ubuntu-22.04'
        uses: actions/upload-artifact@v4
        with:
          name: linux
          path: |
            src-tauri/target/release/bundle/deb/*.deb
            src-tauri/target/release/bundle/appimage/*.AppImage
            src-tauri/target/release/bundle/rpm/*.rpm

      - name: Upload artifacts (Windows)
        if: matrix.platform == 'windows-latest'
        uses: actions/upload-artifact@v4
        with:
          name: windows
          path: |
            src-tauri/target/release/bundle/msi/*.msi
            src-tauri/target/release/bundle/nsis/*.exe

  build-android:
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Install pnpm
        uses: pnpm/action-setup@v4

      - name: Setup Java
        uses: actions/setup-java@v4
        with:
          distribution: 'temurin'
          java-version: '17'

      - name: Setup Android SDK
        uses: android-actions/setup-android@v3

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

      - name: Install Rust Android targets
        run: |
          rustup target add aarch64-linux-android
          rustup target add armv7-linux-androideabi
          rustup target add i686-linux-android
          rustup target add x86_64-linux-android

      - name: Setup NDK
        uses: nttld/setup-ndk@v1
        with:
          ndk-version: r26d
        id: setup-ndk

      - name: Setup Android NDK environment for OpenSSL
        run: |
          echo "ANDROID_NDK_HOME=${{ steps.setup-ndk.outputs.ndk-path }}" >> $GITHUB_ENV
          echo "NDK_HOME=${{ steps.setup-ndk.outputs.ndk-path }}" >> $GITHUB_ENV

          # Add all Android toolchains to PATH for OpenSSL cross-compilation
          echo "${{ steps.setup-ndk.outputs.ndk-path }}/toolchains/llvm/prebuilt/linux-x86_64/bin" >> $GITHUB_PATH

          # Set CC, AR, RANLIB for each target
          echo "CC_aarch64_linux_android=aarch64-linux-android24-clang" >> $GITHUB_ENV
          echo "AR_aarch64_linux_android=llvm-ar" >> $GITHUB_ENV
          echo "RANLIB_aarch64_linux_android=llvm-ranlib" >> $GITHUB_ENV

          echo "CC_armv7_linux_androideabi=armv7a-linux-androideabi24-clang" >> $GITHUB_ENV
          echo "AR_armv7_linux_androideabi=llvm-ar" >> $GITHUB_ENV
          echo "RANLIB_armv7_linux_androideabi=llvm-ranlib" >> $GITHUB_ENV

          echo "CC_i686_linux_android=i686-linux-android24-clang" >> $GITHUB_ENV
          echo "AR_i686_linux_android=llvm-ar" >> $GITHUB_ENV
          echo "RANLIB_i686_linux_android=llvm-ranlib" >> $GITHUB_ENV

          echo "CC_x86_64_linux_android=x86_64-linux-android24-clang" >> $GITHUB_ENV
          echo "AR_x86_64_linux_android=llvm-ar" >> $GITHUB_ENV
          echo "RANLIB_x86_64_linux_android=llvm-ranlib" >> $GITHUB_ENV

      - name: Install build dependencies for OpenSSL
        run: |
          sudo apt-get update
          sudo apt-get install -y perl make

      - name: Get pnpm store directory
        shell: bash
        run: |
          echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV

      - name: Setup pnpm cache
        uses: actions/cache@v4
        with:
          path: ${{ env.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-

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

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

      - name: Setup Keystore (if secrets available)
        env:
          ANDROID_KEYSTORE: ${{ secrets.ANDROID_KEYSTORE }}
          ANDROID_KEYSTORE_PASSWORD: ${{ secrets.ANDROID_KEYSTORE_PASSWORD }}
          ANDROID_KEY_ALIAS: ${{ secrets.ANDROID_KEY_ALIAS }}
          ANDROID_KEY_PASSWORD: ${{ secrets.ANDROID_KEY_PASSWORD }}
        run: |
          if [ -n "$ANDROID_KEYSTORE" ]; then
            echo "$ANDROID_KEYSTORE" | base64 -d > $HOME/keystore.jks
            echo "ANDROID_KEYSTORE_PATH=$HOME/keystore.jks" >> $GITHUB_ENV
            echo "ANDROID_KEYSTORE_PASSWORD=$ANDROID_KEYSTORE_PASSWORD" >> $GITHUB_ENV
            echo "ANDROID_KEY_ALIAS=$ANDROID_KEY_ALIAS" >> $GITHUB_ENV
            echo "ANDROID_KEY_PASSWORD=$ANDROID_KEY_PASSWORD" >> $GITHUB_ENV
            echo "Keystore configured for signing"
          else
            echo "No keystore configured, building unsigned APK"
          fi

      - name: Build Android APK and AAB (unsigned if no keystore)
        run: pnpm tauri android build

      - name: Upload Android artifacts
        uses: actions/upload-artifact@v4
        with:
          name: android
          path: |
            src-tauri/gen/android/app/build/outputs/apk/**/*.apk
            src-tauri/gen/android/app/build/outputs/bundle/**/*.aab
