#!/usr/bin/env bash
set -euo pipefail

platform="${1:?platform is required}"
should_release="${2:-false}"
repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"

emit_output() {
  local key="$1"
  local value="$2"

  if [[ -n "${GITHUB_OUTPUT:-}" ]]; then
    printf '%s=%s\n' "$key" "$value" >> "$GITHUB_OUTPUT"
  else
    printf '%s=%s\n' "$key" "$value"
  fi
}

set_build_state() {
  local should_build="$1"
  local reason="$2"

  emit_output "should_build_platform" "$should_build"
  emit_output "skip_reason" "$reason"

  if [[ "$should_build" == "true" ]]; then
    if [[ -n "$reason" ]]; then
      echo "Preflight passed for $platform with warning: $reason"
    else
      echo "Preflight passed for $platform."
    fi
  else
    echo "Skipping $platform lane: $reason"
  fi
}

warn_unsigned_build() {
  local missing="$1"
  echo "::warning::$platform build will run without Apple signing secrets (missing: $missing)." \
    "Output artifacts will be unsigned/ad-hoc. Configure the Apple signing secrets to enable signed packaging."
}

case "$platform" in
  linux)
    set_build_state "true" ""
    ;;
  windows)
    set_build_state "true" ""
    ;;
  macos)
    required_vars=(
      APPLE_CERT_P12_BASE64
      APPLE_CERT_PASSWORD
      APPLE_KEYCHAIN_PASSWORD
    )

    missing=()
    for var_name in "${required_vars[@]}"; do
      if [[ -z "${!var_name:-}" ]]; then
        missing+=("$var_name")
      fi
    done

    if [[ "${#missing[@]}" -gt 0 ]]; then
      warn_unsigned_build "${missing[*]}"
      set_build_state "true" "missing macOS signing secrets: ${missing[*]}; will produce unsigned DMG"
      exit 0
    fi

    set_build_state "true" ""
    ;;
  ios)
    required_vars=(
      APPLE_CERT_P12_BASE64
      APPLE_CERT_PASSWORD
      APPLE_PROVISION_PROFILE_BASE64
      APPLE_KEYCHAIN_PASSWORD
    )

    missing=()
    for var_name in "${required_vars[@]}"; do
      if [[ -z "${!var_name:-}" ]]; then
        missing+=("$var_name")
      fi
    done

    if [[ "${#missing[@]}" -gt 0 ]]; then
      warn_unsigned_build "${missing[*]}"
      set_build_state "true" "missing iOS signing secrets: ${missing[*]}; will produce unsigned app bundle"
      exit 0
    fi

    set_build_state "true" ""
    ;;
  android)
    set_build_state "true" ""
    ;;
  *)
    echo "Unsupported platform: $platform" >&2
    exit 1
    ;;
esac
