#!/bin/bash

# ===============================
# Flutter APK Build Script (AAB Disabled)
# ===============================

# Exit immediately if a command exits with a non-zero status
set -e

# Colors for pretty output
GREEN='\033[0;32m'
CYAN='\033[0;36m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# App info
APP_NAME="On Field Work"
BUILD_DIR="build/app/outputs"

echo -e "${CYAN}🚀 Starting Flutter build script for $APP_NAME...${NC}"

# Step 1: Clean previous builds
echo -e "${YELLOW}🧹 Cleaning previous builds...${NC}"
flutter clean

# Step 2: Get dependencies
echo -e "${YELLOW}📦 Fetching dependencies...${NC}"
flutter pub get

# ==============================
# Step 3: Build AAB (Commented)
# ==============================
# echo -e "${CYAN}🏗 Building AAB file...${NC}"
# flutter build appbundle --release

# Step 4: Build APK
echo -e "${CYAN}🏗 Building APK file...${NC}"
flutter build apk --release

# Step 5: Show output paths
# AAB_PATH="$BUILD_DIR/bundle/release/app-release.aab"
APK_PATH="$BUILD_DIR/apk/release/app-release.apk"

echo -e "${GREEN}✅ Build completed successfully!${NC}"
# echo -e "${YELLOW}📍 AAB file: ${CYAN}$AAB_PATH${NC}"
echo -e "${YELLOW}📍 APK file: ${CYAN}$APK_PATH${NC}"

# Optional: open the folder (Mac/Linux)
if command -v xdg-open &> /dev/null
then
    xdg-open "$BUILD_DIR"
elif command -v open &> /dev/null
then
    open "$BUILD_DIR"
fi
