name: 'Prepare for Build'
description: 'Setup all required dependencies and secrets for building the given platform'

inputs:
  platform:
    description: 'Platform to be Built'
    required: true

  # Config
  firebase-token:
    description: 'Firebase Token'
    required: false
    default: ''
  skip-core:
    description: 'Skip Installing Core Files'
    required: false
    default: 'false'

  # Secret Keys
  android-key-jks:
    description: 'Android Signing Key'
    required: false
    default: ''
  android-key-properties:
    description: 'Android Signing Key Properties'
    required: false
    default: ''
  appstore-connect-key:
    description: 'App Store Connect API Key'
    required: false
    default: ''
  code-signing-certificate:
    description: 'Code Signing Certificate'
    required: false
    default: ''
  firebase-service-worker:
    description: 'Firebase Service Worker'
    required: false
    default: ''
  google-play-key:
    description: 'Google Play Store API Key'
    required: false
    default: ''
  match-ssh-private-key:
    description: 'Fastlane Match SSH Key'
    required: false
    default: ''

runs:
  using: composite
  steps:
    - name: Checkout Repository
      uses: actions/checkout@v3
      with:
        fetch-depth: 0
        
    - name: Prepare Core
      if: ${{ inputs.skip-core == 'false' }}
      uses: actions/download-artifact@v3
      with:
        name: core-files

    - name: Prepare Folders
      shell: bash
      run: |
        mkdir -p output
        mkdir -p keys

    - name: Install Flutter
      uses: subosito/flutter-action@v2
      with:
        flutter-version: '3.3.7'
        channel: stable
        cache: true
        cache-key: ${{ github.sha }}

    - name: Install Java
      if: ${{ inputs.platform == 'android' }}
      uses: actions/setup-java@v2
      with:
        distribution: zulu
        java-version: 11

    - name: Install Node.js
      uses: actions/setup-node@v3
      with:
        node-version: 16

    - name: Install Ruby
      if: ${{ inputs.platform == 'android' || inputs.platform == 'ios' || inputs.platform == 'macos' }}
      uses: ruby/setup-ruby@v1
      with:
        bundler-cache: true

    - name: Install XCode
      if: ${{ inputs.platform == 'ios' || inputs.platform == 'macos' }}
      uses: maxim-lobanov/setup-xcode@v1
      with:
        xcode-version: latest-stable

    - name: Setup Docker
      if: ${{ inputs.platform == 'web' }}
      uses: docker/setup-buildx-action@v1

    - name: Setup Match SSH Keys 
      if: ${{ inputs.platform == 'ios' || inputs.platform == 'macos' }}
      uses: webfactory/ssh-agent@v0.5.4
      with:
        ssh-private-key: ${{ inputs.match-ssh-private-key }}

    - name: Setup Secret Keys
      shell: bash
      run: |
        echo ${{ inputs.appstore-connect-key }} | base64 --decode > keys/appstore.p8
        echo ${{ inputs.code-signing-certificate }} | base64 --decode > keys/codesigning.pfx
        echo ${{ inputs.google-play-key }} | base64 --decode > keys/googleplay.json
        echo ${{ inputs.android-key-jks }} | base64 --decode > android/key.jks
        echo ${{ inputs.android-key-properties }} | base64 --decode > android/key.properties

    - name: Setup Firebase Configuration
      if: ${{ inputs.firebase-token != '' }}
      shell: bash
      run: |
        npm install -g firebase-tools
        gem install --user-install xcodeproj
        dart pub global activate flutterfire_cli

        flutterfire configure \
        --project=comettools-lunasea \
        --token=${{ inputs.firebase-token }} \
        --platforms=android,ios,macos,web \
        --android-package-name=app.lunasea.lunasea \
        --ios-bundle-id=app.lunasea.lunasea \
        --macos-bundle-id=app.lunasea.lunasea \
        --out=lib/firebase/options.dart \
        --yes

    - name: Setup Firebase Service Worker
      if: ${{ inputs.firebase-service-worker != '' }}
      shell: bash
      run: echo ${{ inputs.firebase-service-worker }} | base64 --decode > web/firebase-messaging-sw.js

    - name: Setup Android Platform
      if: ${{ inputs.platform == 'android' }}
      shell: bash
      run: |
        (cd android && bundle install)

    - name: Setup iOS Platform
      if: ${{ inputs.platform == 'ios' }}
      shell: bash
      run: |
        (cd ios && bundle install)
      
    - name: Setup Linux Platform
      if: ${{ inputs.platform == 'linux' }}
      shell: bash
      run: |
        flutter config --enable-linux-desktop

    - name: Setup macOS Platform
      if: ${{ inputs.platform == 'macos' }}
      shell: bash
      run: |
        flutter config --enable-macos-desktop
        (cd macos && bundle install)

    - name: Setup All Platforms
      shell: bash
      run: |
        flutter pub get
