#!/bin/bash
# ============================================================================
# Ponshu Room - 4バリアント一括ビルドスクリプト
# consumer × {maita, eiji}  +  business × {maita, eiji}
# Pro版は不要のため含まない
# ============================================================================

set -e

TIMESTAMP=$(date +%Y-%m-%d_%H-%M-%S)
OUTPUT_DIR="build/apk_releases/$TIMESTAMP"
mkdir -p "$OUTPUT_DIR"

# APIキー (.env から読み込む)
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
if [ -f "$SCRIPT_DIR/.env" ]; then
  export $(grep -v '^#' "$SCRIPT_DIR/.env" | xargs)
fi
MAITA_KEY="${MAITA_API_KEY:?MAITA_API_KEY is not set in .env}"
EIJI_KEY="${EIJI_API_KEY:?EIJI_API_KEY is not set in .env}"

GRADLE_FILE="android/app/build.gradle.kts"
BACKUP_FILE="android/app/build.gradle.kts.backup"
cp "$GRADLE_FILE" "$BACKUP_FILE"

cleanup() {
    if [ -f "$BACKUP_FILE" ]; then
        cp "$BACKUP_FILE" "$GRADLE_FILE"
        rm "$BACKUP_FILE"
        echo "Restored build.gradle.kts"
    fi
}
trap cleanup EXIT

# applicationId を Lite に固定（バックアップから毎回復元して書き換え）
set_lite_app_id() {
    cp "$BACKUP_FILE" "$GRADLE_FILE"
    sed -i 's/applicationId = "com.posimai.ponshu_room"/applicationId = "com.posimai.ponshu_room_lite"/' "$GRADLE_FILE"
}

echo "============================================================================"
echo "Ponshu Room - 4 Variant APK Build"
echo "Output: $OUTPUT_DIR"
echo "============================================================================"
echo ""

# --- [1/4] Consumer / Maita ---
echo "[1/4] Building Consumer - Maita..."
set_lite_app_id
flutter build apk --release \
  --dart-define=GEMINI_API_KEY=$MAITA_KEY \
  --dart-define=IS_BUSINESS_APP=false \
  --dart-define=IS_PRO_VERSION=false \
  --dart-define=USE_PROXY=false
cp build/app/outputs/flutter-apk/app-release.apk "$OUTPUT_DIR/ponshu_room_consumer_maita.apk"
echo "Saved: ponshu_room_consumer_maita.apk"
echo ""

# --- [2/4] Consumer / Eiji ---
echo "[2/4] Building Consumer - Eiji..."
set_lite_app_id
flutter build apk --release \
  --dart-define=GEMINI_API_KEY=$EIJI_KEY \
  --dart-define=IS_BUSINESS_APP=false \
  --dart-define=IS_PRO_VERSION=false \
  --dart-define=USE_PROXY=false
cp build/app/outputs/flutter-apk/app-release.apk "$OUTPUT_DIR/ponshu_room_consumer_eiji.apk"
echo "Saved: ponshu_room_consumer_eiji.apk"
echo ""

# --- [3/4] Business / Maita ---
echo "[3/4] Building Business - Maita..."
set_lite_app_id
flutter build apk --release \
  --dart-define=GEMINI_API_KEY=$MAITA_KEY \
  --dart-define=IS_BUSINESS_APP=true \
  --dart-define=IS_PRO_VERSION=false \
  --dart-define=USE_PROXY=false
cp build/app/outputs/flutter-apk/app-release.apk "$OUTPUT_DIR/ponshu_room_business_maita.apk"
echo "Saved: ponshu_room_business_maita.apk"
echo ""

# --- [4/4] Business / Eiji ---
echo "[4/4] Building Business - Eiji..."
set_lite_app_id
flutter build apk --release \
  --dart-define=GEMINI_API_KEY=$EIJI_KEY \
  --dart-define=IS_BUSINESS_APP=true \
  --dart-define=IS_PRO_VERSION=false \
  --dart-define=USE_PROXY=false
cp build/app/outputs/flutter-apk/app-release.apk "$OUTPUT_DIR/ponshu_room_business_eiji.apk"
echo "Saved: ponshu_room_business_eiji.apk"
echo ""

echo "============================================================================"
echo "All 4 variants built successfully!"
echo "============================================================================"
ls -lh "$OUTPUT_DIR"
echo ""
echo "Consumer APKs: IS_BUSINESS_APP=false (ビジネスモードトグル非表示)"
echo "Business APKs: IS_BUSINESS_APP=true  (デモ・ヒアリング用)"
echo "============================================================================"
