#!/usr/bin/env bash
#
# Build and push AniWorld container images to the Gitea registry.
#
# Usage:
#   ./push.sh              # builds & pushes app with tag "latest"
#   ./push.sh app          # builds & pushes app image
#   ./push.sh vpn          # builds & pushes vpn image
#   ./push.sh all          # builds & pushes both images
#   ./push.sh app v1.2.3   # builds & pushes app with tag "v1.2.3"
#   ./push.sh vpn v1.2.3   # builds & pushes vpn with tag "v1.2.3"
#   ./push.sh all v1.2.3   # builds & pushes both images
#   ./push.sh app v1.2.3 --no-build   # pushes existing image only
#
# Prerequisites:
#   podman login git.lpl-mind.de   (or: docker login git.lpl-mind.de)

set -euo pipefail

# ---------------------------------------------------------------------------
# Configuration
# ---------------------------------------------------------------------------
REGISTRY="git.lpl-mind.de"
NAMESPACE="lukas.pupkalipinski"
PROJECT="aniworld"

APP_IMAGE="${REGISTRY}/${NAMESPACE}/${PROJECT}/app"
VPN_IMAGE="${REGISTRY}/${NAMESPACE}/${PROJECT}/vpn"

# Parse arguments
TARGET="${1:-app}"
TAG="${2:-latest}"
SKIP_BUILD=false
if [[ "${3:-}" == "--no-build" ]]; then
    SKIP_BUILD=true
fi

# Validate target
if [[ "${TARGET}" != "app" && "${TARGET}" != "vpn" && "${TARGET}" != "all" ]]; then
    echo "ERROR: Invalid target '${TARGET}'. Must be one of: app, vpn, all" >&2
    exit 1
fi

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

# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
log() { echo -e "\n>>> $*"; }
err() { echo -e "\nERROR: $*" >&2; exit 1; }

# Detect container engine (podman preferred, docker fallback)
if command -v podman &>/dev/null; then
    ENGINE="podman"
elif command -v docker &>/dev/null; then
    ENGINE="docker"
else
    err "Neither podman nor docker is installed."
fi

# ---------------------------------------------------------------------------
# Pre-flight checks
# ---------------------------------------------------------------------------
echo "============================================"
echo " AniWorld — Build & Push"
echo " Engine   : ${ENGINE}"
echo " Registry : ${REGISTRY}"
echo " Target   : ${TARGET}"
echo " Tag      : ${TAG}"
echo "============================================"

log "Logging in to ${REGISTRY}"
"${ENGINE}" login "${REGISTRY}"

# ---------------------------------------------------------------------------
# Build
# ---------------------------------------------------------------------------
build_app() {
    log "Building app image  →  ${APP_IMAGE}:${TAG}"
    "${ENGINE}" build \
        -t "${APP_IMAGE}:${TAG}" \
        -f "${SCRIPT_DIR}/Dockerfile.app" \
        "${PROJECT_ROOT}"
}

build_vpn() {
    log "Building vpn image  →  ${VPN_IMAGE}:${TAG}"
    "${ENGINE}" build \
        -t "${VPN_IMAGE}:${TAG}" \
        -f "${SCRIPT_DIR}/Containerfile" \
        "${SCRIPT_DIR}"
}

if [[ "${SKIP_BUILD}" == false ]]; then
    case "${TARGET}" in
        app)  build_app ;;
        vpn)  build_vpn ;;
        all)  build_app; build_vpn ;;
    esac
fi

# ---------------------------------------------------------------------------
# Push
# ---------------------------------------------------------------------------
push_app() {
    log "Pushing ${APP_IMAGE}:${TAG}"
    "${ENGINE}" push "${APP_IMAGE}:${TAG}"
}

push_vpn() {
    log "Pushing ${VPN_IMAGE}:${TAG}"
    "${ENGINE}" push "${VPN_IMAGE}:${TAG}"
}

case "${TARGET}" in
    app)  push_app ;;
    vpn)  push_vpn ;;
    all)  push_app; push_vpn ;;
esac

# ---------------------------------------------------------------------------
# Summary
# ---------------------------------------------------------------------------
echo ""
echo "============================================"
echo "  Push complete!"
echo ""
echo " Images:"
case "${TARGET}" in
    app)  echo "   ${APP_IMAGE}:${TAG}" ;;
    vpn)  echo "   ${VPN_IMAGE}:${TAG}" ;;
    all)  echo "   ${APP_IMAGE}:${TAG}"; echo "   ${VPN_IMAGE}:${TAG}" ;;
esac
echo ""
echo " Deploy on server:"
echo "   ${ENGINE} login ${REGISTRY}"
echo "   ${ENGINE} compose -f Docker/podman-compose.prod.yml pull"
echo "   ${ENGINE} compose -f Docker/podman-compose.prod.yml up -d"
echo "============================================"