####
# Dockerfile de production pour UnionFlow Client (Frontend)
# Build depuis la racine du monorepo
####

## Stage 1 : Build avec Maven
FROM maven:3.9.6-eclipse-temurin-17 AS builder

WORKDIR /app

# Copier tous les POMs du monorepo
COPY pom.xml .
COPY unionflow-server-api/pom.xml unionflow-server-api/
COPY unionflow-client-quarkus-primefaces-freya/pom.xml unionflow-client-quarkus-primefaces-freya/

# Télécharger les dépendances
RUN mvn dependency:go-offline -B

# Copier le code source
COPY unionflow-server-api/src unionflow-server-api/src
COPY unionflow-client-quarkus-primefaces-freya/src unionflow-client-quarkus-primefaces-freya/src

# Construire l'application
RUN mvn clean package -DskipTests -B -Dquarkus.profile=prod -pl unionflow-client-quarkus-primefaces-freya -am

## Stage 2 : Image de production
FROM eclipse-temurin:17-jre-alpine

ENV LANGUAGE='fr_FR:fr'
ENV QUARKUS_PROFILE=prod
ENV QUARKUS_HTTP_PORT=8086
ENV QUARKUS_HTTP_HOST=0.0.0.0

# Variables d'environnement pour production
ENV QUARKUS_OIDC_AUTH_SERVER_URL=https://security.lions.dev/realms/unionflow
ENV QUARKUS_OIDC_CLIENT_ID=unionflow-client
ENV QUARKUS_OIDC_ENABLED=true
ENV QUARKUS_OIDC_TLS_VERIFICATION=required
ENV KEYCLOAK_CLIENT_SECRET=unionflow-client-secret-2025

ENV UNIONFLOW_BACKEND_URL=https://api.lions.dev/unionflow

ENV QUARKUS_HTTP_CORS_ORIGINS=https://unionflow.lions.dev,https://security.lions.dev
ENV QUARKUS_HTTP_CORS_ALLOW_CREDENTIALS=true

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

# Créer utilisateur non-root
RUN addgroup -g 185 -S appuser && adduser -u 185 -S appuser -G appuser
RUN mkdir -p /app/logs && chown -R appuser:appuser /app/logs

USER appuser

# Copier l'application
COPY --from=builder --chown=appuser:appuser /app/unionflow-client-quarkus-primefaces-freya/target/quarkus-app/ /deployments/

EXPOSE 8086

ENV JAVA_OPTS="-Xmx768m -Xms256m \
    -XX:+UseG1GC \
    -XX:MaxGCPauseMillis=200 \
    -XX:+UseStringDeduplication \
    -Djava.security.egd=file:/dev/./urandom \
    -Dquarkus.profile=${QUARKUS_PROFILE}"

HEALTHCHECK --interval=30s --timeout=10s --start-period=90s --retries=3 \
    CMD curl -f http://localhost:8086/q/health/ready || exit 1

ENTRYPOINT ["sh", "-c", "exec java $JAVA_OPTS -jar /deployments/quarkus-run.jar"]
