#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

ask_yes_no() {
  local answer
  read -rp "$1 [y/N]: " answer
  [[ "$answer" =~ ^[Yy]([Ee][Ss])?$ ]]
}

fail() { echo "[FAIL] $*" >&2; exit 1; }

MANIFEST="install-manifest.json"
started_at="$(date -u +%FT%TZ)"
write_manifest() {
  local api_status="$1" apps_status="$2" ai_status="$3" ai_storage="${4:-none}"
  local version="${VERSION:-latest}" registry="${REGISTRY:-korobosta}"
  umask 077
  cat > "$MANIFEST" <<EOF
{
  "schema_version": 1,
  "installation_started_at": "$started_at",
  "last_updated_at": "$(date -u +%FT%TZ)",
  "host": "$(hostname 2>/dev/null || echo unknown)",
  "components": {
    "apis": {"status": "$api_status", "image_registry": "$registry", "image_version": "$version},
    "apps": {"status": "$apps_status", "profiles": "all"},
    "ai": {"status": "$ai_status", "storage_mode": "$ai_storage"}
  },
  "secrets_recorded": false
}
EOF
}

[[ -f apis/install.sh ]] || fail "apis/install.sh is missing"

install_apis=false
install_apps=false
install_ai=false
api_status=not_selected
apps_status=not_selected
ai_status=not_selected
ai_storage=none
if [[ "${KORO_NONINTERACTIVE:-}" == "1" ]]; then
  install_apis=true
else
  echo "Koro ERP installation components"
  ask_yes_no "Install ERP APIs and core services?" && install_apis=true
  ask_yes_no "Install optional web apps (POS, Staff, Shop, School, Merchants, Partners)?" && install_apps=true
  ask_yes_no "Install AI service on this server?" && install_ai=true
fi

$install_apps && ! $install_apis && fail "Web apps require the ERP APIs/core services."
write_manifest "$api_status" "$apps_status" "$ai_status" "$ai_storage"

if $install_apis; then
  bash apis/install.sh
  api_status=installed

export KORO_INSTALL_APPS="$install_apps"
  write_manifest "$api_status" "$apps_status" "$ai_status" "$ai_storage"
fi

if $install_apps; then
  [[ -f apis/.env ]] || fail "apis/.env is missing; install APIs first"
  docker compose --env-file apis/.env -f apis/docker-compose.yml -f apps/docker-compose.yml --profile all up -d
  apps_status=installed
  write_manifest "$api_status" "$apps_status" "$ai_status" "$ai_storage"
fi

if $install_ai; then
  if $install_apis && [[ -f apis/.env ]]; then
    export KORO_DB_HOST="$(sed -n 's/^DB_HOST=//p' apis/.env | tail -n1)"
    export KORO_DB_PORT="$(sed -n 's/^DB_PORT=//p' apis/.env | tail -n1)"
    export KORO_DB_USER="$(sed -n 's/^DB_USER=//p' apis/.env | tail -n1)"
    export KORO_DB_PASSWORD="$(sed -n 's/^DB_PASSWORD=//p' apis/.env | tail -n1)"
  fi
  bash ai/install.sh
  ai_status=installed
  if [[ -f ai/.env.ai ]]; then
    ai_storage="$(grep -E '^STORAGE_MODE=' ai/.env.ai | cut -d= -f2- || echo unknown)"
  fi
  write_manifest "$api_status" "$apps_status" "$ai_status" "$ai_storage"
fi

echo "[OK] Installation cycle complete"
echo ""
echo "Installed components:"
$install_apis && echo "  APIs: installed"
$install_apps && echo "  Web apps: installed"
$install_ai && echo "  AI: installed"
echo ""
echo "Next steps:"
if $install_apis && ! $install_apps; then
  echo "  Web apps were not installed. Install them here or on another server using the API URL above."
fi
if $install_apis && ! $install_ai; then
  echo "  AI was not installed. Install it later on this server or a separate VM."
fi
if $install_ai && ! $install_apis; then
  echo "  APIs were not installed. Link this AI service to an existing ERP gateway."
fi
if $install_ai && $install_apis; then
  echo "  To connect a separately hosted AI service later: ./manage.sh link-ai http://AI-SERVER:7025"
fi
