# Kindly Flutter plugin example app — local run helpers
#
# This is the DEVELOPER-SIDE smoke test inside the plugin: depends on
# `kindly: { path: ../ }`, so any local edit to flutter-sdk/lib/, ios/,
# or android/ shows up here without republishing.
#
# Use this when iterating on the plugin bridge. For a host-app integration
# test (depends on `kindly: ^x.y.z` from pub.dev), see flutter-sample/.

PROJECT_DIR ?= $(PWD)
DEVICE_ID   ?=

ifeq ($(DEVICE_ID),)
RUN_FLAGS =
else
RUN_FLAGS = -d $(DEVICE_ID)
endif

.PHONY: help setup get clean format analyze test \
        run-ios run-android run-ios-release run-android-release \
        build-ios build-android \
        list-devices list-emulators \
        boot-ios-simulator boot-android-emulator

help:
	@echo "Kindly Flutter plugin Example app — make targets"
	@echo ""
	@echo "  setup               flutter pub get + iOS pod install"
	@echo "  get                 flutter pub get"
	@echo "  clean               flutter clean (.dart_tool, build/, Pods/)"
	@echo ""
	@echo "  list-devices        Show all attached + simulator devices"
	@echo "  list-emulators      Show available Android emulators"
	@echo "  boot-ios-simulator  Open the default iOS simulator"
	@echo "  boot-android-emulator  Boot the first Android AVD"
	@echo ""
	@echo "  run-ios             flutter run on first iOS device/sim"
	@echo "                      (override: make run-ios DEVICE_ID=<id>)"
	@echo "  run-android         flutter run on first Android device/emulator"
	@echo "  run-ios-release     flutter run --release on iOS"
	@echo "  run-android-release flutter run --release on Android"
	@echo ""
	@echo "  build-ios           flutter build ios --no-codesign"
	@echo "  build-android       flutter build apk --debug"
	@echo ""
	@echo "  analyze             flutter analyze"
	@echo "  test                flutter test"
	@echo "  format              dart format lib/"

# ---------------------------------------------------------------------------
# Setup
# ---------------------------------------------------------------------------
setup: get
	@if [ -d ios ]; then \
		echo "🔧 Running pod install in ios/..."; \
		cd ios && pod install --repo-update 2>&1 | tail -10; \
	fi
	@echo "🎉 Setup complete."

get:
	@flutter pub get

clean:
	@flutter clean
	@cd ios 2>/dev/null && rm -rf Pods Podfile.lock && cd ..
	@echo "✅ Cleaned."

# ---------------------------------------------------------------------------
# Device discovery
# ---------------------------------------------------------------------------
list-devices:
	@flutter devices

list-emulators:
	@flutter emulators

boot-ios-simulator:
	@open -a Simulator
	@echo "✅ iOS Simulator launched (use list-devices to see it)."

boot-android-emulator:
	@AVD=$$(flutter emulators 2>/dev/null | grep -E "^[a-zA-Z0-9_]+ +• .*android" | head -1 | awk '{print $$1}'); \
	if [ -z "$$AVD" ]; then \
		echo "❌ No Android emulators found. Create one in Android Studio first."; \
		exit 1; \
	fi; \
	echo "🚀 Booting AVD: $$AVD"; \
	flutter emulators --launch "$$AVD"

# ---------------------------------------------------------------------------
# Run
# ---------------------------------------------------------------------------
run-ios:
	@DEV=$$(flutter devices --machine 2>/dev/null | python3 -c "import json,sys; ds=[d for d in json.load(sys.stdin) if d.get('targetPlatform','').startswith('ios')]; print(ds[0]['id']) if ds else exit(1)" 2>/dev/null); \
	if [ -z "$$DEV" ]; then \
		echo "❌ No iOS device/simulator. Run: make boot-ios-simulator"; exit 1; \
	fi; \
	echo "📱 Running on iOS device: $$DEV"; \
	flutter run -d "$$DEV" $(RUN_FLAGS)

run-android:
	@DEV=$$(flutter devices --machine 2>/dev/null | python3 -c "import json,sys; ds=[d for d in json.load(sys.stdin) if d.get('targetPlatform','').startswith('android')]; print(ds[0]['id']) if ds else exit(1)" 2>/dev/null); \
	if [ -z "$$DEV" ]; then \
		echo "❌ No Android device/emulator. Run: make boot-android-emulator"; exit 1; \
	fi; \
	echo "🤖 Running on Android device: $$DEV"; \
	flutter run -d "$$DEV" $(RUN_FLAGS)

run-ios-release:
	@flutter run --release $(RUN_FLAGS)

run-android-release:
	@flutter run --release $(RUN_FLAGS)

# ---------------------------------------------------------------------------
# Build
# ---------------------------------------------------------------------------
build-ios:
	@flutter build ios --no-codesign

build-android:
	@flutter build apk --debug

# ---------------------------------------------------------------------------
# Lint / test
# ---------------------------------------------------------------------------
analyze:
	@flutter analyze

test:
	@flutter test

format:
	@dart format lib/
