#!/bin/bash

# Allam Mobile - App Store Build & Submit Script
# This script automates building and submitting to app stores

set -e  # Exit on error

echo "🚀 Allam Mobile - Build & Submit Script"
echo "========================================"

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

# Check if EAS CLI is installed
if ! command -v eas &> /dev/null; then
    echo -e "${RED}❌ EAS CLI not found. Installing...${NC}"
    npm install -g eas-cli
fi

# Check if logged in to EAS
echo -e "${YELLOW}📝 Checking EAS login status...${NC}"
if ! eas whoami &> /dev/null; then
    echo -e "${RED}❌ Not logged in to EAS. Please login:${NC}"
    eas login
fi

# Function to build Android
build_android() {
    echo -e "${GREEN}🤖 Building Android App Bundle (AAB)...${NC}"
    echo "This will take 15-30 minutes on EAS cloud build."
    
    read -p "Continue with Android build? (y/n) " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        eas build --platform android --profile production --non-interactive
        echo -e "${GREEN}✅ Android build started!${NC}"
        echo "Check progress at: https://expo.dev/accounts/[your-account]/projects/allam-mobile/builds"
    fi
}

# Function to build iOS
build_ios() {
    echo -e "${GREEN}🍎 Building iOS App (IPA)...${NC}"
    echo "This will take 20-40 minutes on EAS cloud build."
    echo -e "${YELLOW}⚠️  Make sure you have:"
    echo "  1. Apple Developer Account (\$99/year)"
    echo "  2. iOS signing certificates configured (run 'eas credentials')"
    echo "  3. App created in App Store Connect"
    echo -e "${NC}"
    
    read -p "Continue with iOS build? (y/n) " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        eas build --platform ios --profile production --non-interactive
        echo -e "${GREEN}✅ iOS build started!${NC}"
        echo "Check progress at: https://expo.dev/accounts/[your-account]/projects/allam-mobile/builds"
    fi
}

# Function to submit to Google Play
submit_android() {
    echo -e "${GREEN}📤 Submitting to Google Play Store...${NC}"
    
    # Check if service account key exists
    if [ ! -f "./google-service-account.json" ]; then
        echo -e "${RED}❌ google-service-account.json not found!${NC}"
        echo "Please:"
        echo "1. Create a service account in Google Cloud Console"
        echo "2. Download the JSON key"
        echo "3. Save it as google-service-account.json in project root"
        return 1
    fi
    
    read -p "Submit to Google Play internal track? (y/n) " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        eas submit --platform android --profile production --non-interactive
        echo -e "${GREEN}✅ Android submission started!${NC}"
    fi
}

# Function to submit to App Store
submit_ios() {
    echo -e "${GREEN}📤 Submitting to Apple App Store...${NC}"
    echo -e "${YELLOW}⚠️  Make sure you have:"
    echo "  1. Completed eas.json with ascAppId and appleTeamId"
    echo "  2. App created in App Store Connect"
    echo "  3. TestFlight testing completed (recommended)"
    echo -e "${NC}"
    
    read -p "Submit to App Store? (y/n) " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        eas submit --platform ios --profile production --non-interactive
        echo -e "${GREEN}✅ iOS submission started!${NC}"
    fi
}

# Function to check app.json configuration
check_config() {
    echo -e "${YELLOW}🔍 Checking app.json configuration...${NC}"
    
    if ! grep -q '"version"' app.json; then
        echo -e "${RED}❌ Version not found in app.json${NC}"
        return 1
    fi
    
    if ! grep -q '"android"' app.json; then
        echo -e "${RED}❌ Android configuration missing in app.json${NC}"
        return 1
    fi
    
    if ! grep -q '"ios"' app.json; then
        echo -e "${RED}❌ iOS configuration missing in app.json${NC}"
        return 1
    fi
    
    echo -e "${GREEN}✅ app.json looks good!${NC}"
    return 0
}

# Function to display menu
show_menu() {
    echo ""
    echo "What would you like to do?"
    echo "  1) Build Android App Bundle (AAB)"
    echo "  2) Build iOS App (IPA)"
    echo "  3) Submit Android to Google Play"
    echo "  4) Submit iOS to App Store"
    echo "  5) Build & Submit Android (full pipeline)"
    echo "  6) Build & Submit iOS (full pipeline)"
    echo "  7) Check configuration"
    echo "  8) Exit"
    echo ""
}

# Main script logic
main() {
    # Check configuration first
    check_config
    
    while true; do
        show_menu
        read -p "Enter your choice [1-8]: " choice
        
        case $choice in
            1) build_android ;;
            2) build_ios ;;
            3) submit_android ;;
            4) submit_ios ;;
            5) 
                build_android
                echo ""
                submit_android
                ;;
            6)
                build_ios
                echo ""
                submit_ios
                ;;
            7) check_config ;;
            8) 
                echo -e "${GREEN}👋 Goodbye!${NC}"
                exit 0
                ;;
            *) 
                echo -e "${RED}❌ Invalid option${NC}"
                ;;
        esac
        
        echo ""
        read -p "Press Enter to continue..."
    done
}

# Run main function
main
