name: Mobile Build

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
  workflow_dispatch:

jobs:
  build-ios:
    runs-on: macos-latest
    steps:
      - uses: actions/checkout@v3
      
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: aarch64-apple-ios, x86_64-apple-ios
      
      - name: Cache Rust dependencies
        uses: Swatinem/rust-cache@v2
      
      - name: Create iOS simulator linker shim
        run: |
          SDK_PATH=$(xcrun --sdk iphonesimulator --show-sdk-path)
          echo "#!/bin/sh" > linker_shim.sh
          echo "cc -isysroot $SDK_PATH \"\$@\"" >> linker_shim.sh
          chmod +x linker_shim.sh
          echo "CARGO_TARGET_X86_64_APPLE_IOS_LINKER=$(pwd)/linker_shim.sh" >> $GITHUB_ENV
      
      - name: Build for iOS Simulator
        run: |
          cargo build --release --example test --target x86_64-apple-ios
      
      - name: Package iOS app
        run: |
          mkdir -p ios-app/tinyfiledialogs.app
          cp target/x86_64-apple-ios/release/examples/test ios-app/tinyfiledialogs.app/tinyfiledialogs
          
          cat > ios-app/tinyfiledialogs.app/Info.plist << EOF
          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
          <plist version="1.0">
          <dict>
            <key>CFBundleExecutable</key>
            <string>tinyfiledialogs</string>
            <key>CFBundleIdentifier</key>
            <string>com.example.tinyfiledialogs</string>
            <key>CFBundleName</key>
            <string>TinyFileDialogs</string>
            <key>CFBundleVersion</key>
            <string>1.0</string>
            <key>CFBundleShortVersionString</key>
            <string>1.0</string>
            <key>UIRequiredDeviceCapabilities</key>
            <array>
              <string>x86_64</string>
            </array>
          </dict>
          </plist>
          EOF
      
      - name: Upload iOS artifacts
        uses: actions/upload-artifact@v4
        with:
          name: ios-simulator-app
          path: ios-app/

  build-android:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      
      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: armv7-linux-androideabi, aarch64-linux-android, i686-linux-android, x86_64-linux-android
      
      - name: Cache Rust dependencies
        uses: Swatinem/rust-cache@v2
      
      - name: Setup Android SDK
        uses: android-actions/setup-android@v2
      
      - name: Install Android NDK
        run: |
          sdkmanager --install "ndk;25.2.9519653"
          echo "ANDROID_NDK_HOME=$ANDROID_SDK_ROOT/ndk/25.2.9519653" >> $GITHUB_ENV
      
      - name: Setup cargo-ndk
        run: |
          cargo install cargo-ndk
      
      - name: Create Android Project Structure
        run: |
          mkdir -p android-project/app/src/main/java/com/example/tinyfiledialogs
          mkdir -p android-project/app/src/main/jniLibs/{armeabi-v7a,arm64-v8a,x86,x86_64}
          
          # Create DialogHelper.java
          cat > android-project/app/src/main/java/com/example/tinyfiledialogs/DialogHelper.java << EOF
          package com.example.tinyfiledialogs;
          
          // Copy the full DialogHelper.java content here
          // (Abbreviated for brevity in this workflow file)
          public class DialogHelper {
              // Implementation methods
          }
          EOF
      
      - name: Build for Android
        run: |
          # Build for all target architectures
          cargo ndk -t armeabi-v7a -t arm64-v8a -t x86 -t x86_64 -o android-project/app/src/main/jniLibs build --release --example test
      
      - name: Create Android APK structure
        run: |
          mkdir -p android-project/app/src/main/res/layout
          mkdir -p android-project/app/src/main/res/values
          
          # Create basic AndroidManifest.xml
          cat > android-project/app/src/main/AndroidManifest.xml << EOF
          <?xml version="1.0" encoding="utf-8"?>
          <manifest xmlns:android="http://schemas.android.com/apk/res/android"
              package="com.example.tinyfiledialogs">
          
              <application
                  android:allowBackup="true"
                  android:label="TinyFileDialogs"
                  android:supportsRtl="true">
                  <activity
                      android:name=".MainActivity"
                      android:exported="true">
                      <intent-filter>
                          <action android:name="android.intent.action.MAIN" />
                          <category android:name="android.intent.category.LAUNCHER" />
                      </intent-filter>
                  </activity>
              </application>
          
          </manifest>
          EOF
          
          # Create MainActivity.java
          cat > android-project/app/src/main/java/com/example/tinyfiledialogs/MainActivity.java << EOF
          package com.example.tinyfiledialogs;
          
          import android.app.Activity;
          import android.os.Bundle;
          
          public class MainActivity extends Activity {
              static {
                  System.loadLibrary("test");
              }
              
              @Override
              protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  // Initialize DialogHelper
                  DialogHelper.createNotificationChannel(this);
              }
              
              // Native methods would be defined here
              public native void runTest();
          }
          EOF
      
      - name: Upload Android artifacts
        uses: actions/upload-artifact@v4
        with:
          name: android-project
          path: android-project/