#!/bin/bash

# GoLapApp iOS Build für IPA-Erstellung
echo "📱 GoLapApp iOS Build wird erstellt..."

# Projektverzeichnis
PROJECT_DIR="/Users/atakanozkan/GoLapApp"

# Voraussetzungen prüfen
echo "🔍 Voraussetzungen werden geprüft..."
if ! command -v eas &> /dev/null; then
    echo "❌ EAS CLI nicht gefunden. Installiere mit: npm install -g eas-cli"
    exit 1
fi

if ! command -v xcodebuild &> /dev/null; then
    echo "❌ Xcode nicht gefunden. Installiere Xcode von App Store"
    exit 1
fi

# EAS Konfiguration erstellen
echo "⚙️ EAS Konfiguration wird erstellt..."
cat > "$PROJECT_DIR/eas.json" << EOF
{
  "cli": {
    "version": ">= 3.0.0"
  },
  "build": {
    "development": {
      "developmentClient": true,
      "distribution": "internal"
    },
    "preview": {
      "distribution": "internal",
      "ios": {
        "simulator": true
      }
    },
    "production": {
      "ios": {
        "autoIncrement": true
      }
    },
    "sideload": {
      "distribution": "internal",
      "ios": {
        "enterpriseProvisioning": false
      }
    }
  },
  "submit": {
    "production": {}
  }
}
EOF

# App.json Konfiguration
echo "⚙️ App.json wird konfiguriert..."
cat > "$PROJECT_DIR/app.json" << EOF
{
  "expo": {
    "name": "GoLap",
    "slug": "golapapp",
    "version": "1.0.0",
    "orientation": "portrait",
    "icon": "./assets/icon.png",
    "userInterfaceStyle": "automatic",
    "splash": {
      "image": "./assets/splash.png",
      "resizeMode": "contain",
      "backgroundColor": "#1E1E1E"
    },
    "assetBundlePatterns": [
      "**/*"
    ],
    "ios": {
      "supportsTablet": true,
      "bundleIdentifier": "com.golap.app",
      "buildNumber": "1.0.0"
    },
    "android": {
      "adaptiveIcon": {
        "foregroundImage": "./assets/adaptive-icon.png",
        "backgroundColor": "#1E1E1E"
      },
      "package": "com.golap.app"
    },
    "web": {
      "favicon": "./assets/favicon.png",
      "bundler": "metro"
    },
    "plugins": [
      "expo-router"
    ],
    "scheme": "golapapp"
  }
}
EOF

# Assets Verzeichnis erstellen
echo "📁 Assets Verzeichnis wird erstellt..."
mkdir -p "$PROJECT_DIR/assets"

# Build für iOS Sideload
echo "🔨 iOS Build für Sideload wird erstellt..."
cd "$PROJECT_DIR"
eas build --platform ios --profile sideload

echo "✅ iOS Build abgeschlossen!"
echo "📱 IPA-Datei kann in EAS Console heruntergeladen werden"
echo "🔗 https://expo.dev/accounts/[account]/projects/[project]/builds"
