#!/bin/bash
BUILD_NAME="1.0.0"
BUILD_NUMBER="1"
EXPORT_OPTIONS_PLIST="Support/ExportOptions.plist"

flutter clean
flutter pub get
dart run build_runner build --delete-conflicting-outputs

cd ios
rm -rf Pods Podfile.lock
pod install --repo-update
pod update || { echo "Pod update failed"; exit 1; }
echo "Building iOS IPA"

flutter build ipa \
    --build-name="$BUILD_NAME" \
    --build-number="$BUILD_NUMBER" \
    --export-options-plist="$EXPORT_OPTIONS_PLIST" \
    --release \
    -v

BUILD_RESULT=$?
echo "Build exit code: $BUILD_RESULT"
echo "Contents of build directory:"
ls -R build/ios/

if [ $BUILD_RESULT -eq 0 ]; then
    echo "Build completed successfully!"
    echo "IPA file location: ./build/ios/ipa/"
else
    echo "Build failed!"
    echo "Please ensure:"
    echo "1. Valid provisioning profile is configured in Xcode"
    echo "2. exportOptions.plist contains correct provisioning profile information"
    echo "3. Signing certificates are properly installed"
    exit 1
fi
