name: Build and Test

on:
  push:
    paths:
      - 'api/**'
      - 'web/**'
      - 'android/**'
      - '.github/workflows/build-and-test.yaml'
  
  workflow_dispatch:
    inputs:
      branch:
        description: 'Branch to run workflow on'
        required: true
        default: 'main'
        type: string
      android_variant:
        description: 'Android build variant'
        required: true
        default: 'dev'
        type: choice
        options:
          - dev
          - prod

jobs:
  build-and-test-web-and-api:
    name: Build and Test web and api
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event_name == 'workflow_dispatch' && inputs.branch || github.ref }}
      
      - name: Set up Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '18'
      
      - name: Install pnpm
        uses: pnpm/action-setup@v2
        with:
          version: 8
          run_install: false

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

      - name: Setup pnpm cache
        uses: actions/cache@v3
        with:
          path: ${{ env.STORE_PATH }}
          key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
          restore-keys: |
            ${{ runner.os }}-pnpm-store-
      
      - name: Build and test API
        run: |
          cd api
          pnpm install
          pnpm run build
          pnpm test
      
      - name: Build web
        run: |
          cd web
          pnpm install
          pnpm run build 

  build-and-test-android:
    name: Build and Test Android
    runs-on: ubuntu-latest
    strategy:
      matrix:
        variant: [dev, prod]
    
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          ref: ${{ github.event_name == 'workflow_dispatch' && inputs.branch || github.ref }}
      
      - name: Set up JDK 17
        uses: actions/setup-java@v3
        with:
          java-version: '17'
          distribution: 'temurin'
          cache: gradle
      
      - name: Setup Gradle
        uses: gradle/gradle-build-action@v2
        with:
          gradle-version: '7.2'
      
      - name: Grant execute permission for gradlew
        run: chmod +x android/gradlew
      
      - name: Create debug keystore
        run: |
          mkdir -p ~/.android
          keytool -genkey -v -keystore ~/.android/debug.keystore -storepass android -alias androiddebugkey -keypass android -keyalg RSA -keysize 2048 -validity 10000 -dname "CN=Android Debug,O=Android,C=US"
      
      - name: Create google-services.json for dev
        if: matrix.variant == 'dev'
        run: |
          mkdir -p android/app/src/dev
          echo '${{ secrets.GOOGLE_SERVICES_JSON_DEV }}' > android/app/src/dev/google-services.json

      - name: Create google-services.json for prod
        if: matrix.variant == 'prod'
        run: |
          mkdir -p android/app/src/prod
          echo '${{ secrets.GOOGLE_SERVICES_JSON_PROD }}' > android/app/src/prod/google-services.json

      - name: Build Android app
        run: |
          cd android
          ./gradlew assemble${{ matrix.variant }}Debug
      
      # - name: Run Android tests
      #   run: |
      #     cd android
      #     ./gradlew test${{ matrix.variant }}DebugUnitTest
      
      - name: Sanitize ref name for artifact
        shell: bash
        run: echo "SAFE_REF_NAME=${GITHUB_REF_NAME//\//-}" >> $GITHUB_ENV
      
      - name: Upload build artifacts
        uses: actions/upload-artifact@v4
        with:
          name: textbee-${{ matrix.variant }}-build-${{ env.SAFE_REF_NAME }}-${{ github.sha }}.apk
          path: android/app/build/outputs/apk/${{ matrix.variant }}/debug/app-${{ matrix.variant }}-debug.apk
          retention-days: 7 