#!/bin/bash

# Define relative path coordinates (adjusted for monorepo structure)
ANDROID_OUTPUT_PATH="./frontend/android/app/build/outputs/apk/release/app-release.apk"
BACKEND_TARGET_DIR="./backend/public/downloads"
TARGET_FILE_NAME="yotrip-latest.apk"

echo "🚀 Starting automated post-build Android deployment pipeline..."

# 1. Verify compiler target exists
if [ -f "$ANDROID_OUTPUT_PATH" ]; then
    # 2. Ensure target storage folder structure is active
    mkdir -p "$BACKEND_TARGET_DIR"

    # 3. Copy and force overwrite the old production bundle with the updated version
    cp -f "$ANDROID_OUTPUT_PATH" "$BACKEND_TARGET_DIR/$TARGET_FILE_NAME"
    
    echo "✅ Success! New build copied safely to $BACKEND_TARGET_DIR/$TARGET_FILE_NAME"
    echo "🔗 Direct Download Link Active: /downloads/$TARGET_FILE_NAME"
else
    echo "❌ Critical Error: Android build output artifact not found at $ANDROID_OUTPUT_PATH"
    exit 1
fi
