####
# Dockerfile pour UnionFlow Client - Compatible Quarkus 3.x
# Utilise le format quarkus-app de Quarkus 3.x
####

FROM eclipse-temurin:17-jre-alpine

ENV LANGUAGE='en_US:en'

# Configuration Quarkus
ENV QUARKUS_PROFILE=prod
ENV QUARKUS_HTTP_PORT=8086
ENV QUARKUS_HTTP_HOST=0.0.0.0

# Configuration Backend UnionFlow
ENV UNIONFLOW_BACKEND_URL=https://lions.dev/unionflow

# Configuration Keycloak OIDC
# IMPORTANT: Les secrets doivent être injectés via Kubernetes Secrets au runtime
ENV KEYCLOAK_AUTH_SERVER_URL=https://security.lions.dev/realms/unionflow
# ENV KEYCLOAK_CLIENT_SECRET will be injected via Kubernetes Secret

# Créer l'utilisateur appuser
RUN addgroup -g 185 appuser && adduser -D -u 185 -G appuser appuser

# Installer curl pour health checks
RUN apk add --no-cache curl

# Créer les répertoires nécessaires
RUN mkdir -p /app/logs /app/storage && chown -R appuser:appuser /app

WORKDIR /app

# Copier quarkus-app depuis target/ (format Quarkus 3.x)
COPY --chown=appuser:appuser target/quarkus-app/lib/ /app/lib/
COPY --chown=appuser:appuser target/quarkus-app/*.jar /app/
COPY --chown=appuser:appuser target/quarkus-app/app/ /app/app/
COPY --chown=appuser:appuser target/quarkus-app/quarkus/ /app/quarkus/

USER appuser

# Exposer le port 8086
EXPOSE 8086

# Variables JVM optimisées
ENV JAVA_OPTS="-Xmx1g -Xms512m \
    -XX:+UseG1GC \
    -XX:MaxGCPauseMillis=200 \
    -XX:+UseStringDeduplication \
    -XX:+HeapDumpOnOutOfMemoryError \
    -XX:HeapDumpPath=/app/logs/heapdump.hprof \
    -Djava.security.egd=file:/dev/./urandom \
    -Djava.awt.headless=true \
    -Dfile.encoding=UTF-8 \
    -Djava.util.logging.manager=org.jboss.logmanager.LogManager \
    -Dquarkus.profile=${QUARKUS_PROFILE}"

# Point d'entrée pour Quarkus 3.x
ENTRYPOINT ["sh", "-c", "exec java $JAVA_OPTS -jar /app/quarkus-run.jar"]

# Health check sur le bon port
HEALTHCHECK --interval=30s --timeout=10s --start-period=120s --retries=3 \
  CMD curl -f http://localhost:8086/q/health/ready || exit 1
