# Custom OpenSearch Dockerfile with Java 21 for compatibility
FROM amazoncorretto:21-alpine AS java-base

# Download and extract OpenSearch
FROM java-base AS opensearch-builder
WORKDIR /tmp
RUN apk add --no-cache curl tar && \
    curl -L https://artifacts.opensearch.org/releases/bundle/opensearch/3.2.0/opensearch-3.2.0-linux-x64.tar.gz | \
    tar -xz && \
    mv opensearch-3.2.0 /usr/share/opensearch

# Final runtime image
FROM java-base
WORKDIR /usr/share/opensearch

# Create opensearch user
RUN addgroup -g 1000 opensearch && \
    adduser -u 1000 -G opensearch -s /bin/sh -D opensearch

# Copy OpenSearch from builder stage
COPY --from=opensearch-builder --chown=opensearch:opensearch /usr/share/opensearch /usr/share/opensearch

# Install necessary packages
RUN apk add --no-cache bash curl

# Debug: Check Java installation and set correct paths
RUN which java && java -version && \
    ls -la /usr/lib/jvm/ && \
    ln -sf /usr/lib/jvm/java-21-amazon-corretto /usr/lib/jvm/default-jvm

# Set environment variables
ENV JAVA_HOME=/usr/lib/jvm/java-21-amazon-corretto
ENV OPENSEARCH_JAVA_HOME=/usr/lib/jvm/java-21-amazon-corretto
ENV PATH=$PATH:$JAVA_HOME/bin

# Create required directories and disable security plugin
RUN mkdir -p /usr/share/opensearch/data && \
    mkdir -p /usr/share/opensearch/logs && \
    echo "plugins.security.disabled: true" >> /usr/share/opensearch/config/opensearch.yml && \
    echo "discovery.type: single-node" >> /usr/share/opensearch/config/opensearch.yml && \
    echo "cluster.name: storycove-opensearch" >> /usr/share/opensearch/config/opensearch.yml && \
    echo "node.name: opensearch-node" >> /usr/share/opensearch/config/opensearch.yml && \
    echo "bootstrap.memory_lock: false" >> /usr/share/opensearch/config/opensearch.yml && \
    echo "network.host: 0.0.0.0" >> /usr/share/opensearch/config/opensearch.yml && \
    echo "logger.level: DEBUG" >> /usr/share/opensearch/config/opensearch.yml && \
    echo "node.processors: 1" >> /usr/share/opensearch/config/opensearch.yml && \
    rm -rf /usr/share/opensearch/plugins/opensearch-performance-analyzer && \
    rm -rf /usr/share/opensearch/agent && \
    echo "# Custom JVM options for Synology NAS compatibility" > /usr/share/opensearch/config/jvm.options.d/synology.options && \
    echo "-Dlucene.useVectorAPI=false" >> /usr/share/opensearch/config/jvm.options.d/synology.options && \
    echo "-Dorg.apache.lucene.store.MMapDirectory.enableMemorySegments=false" >> /usr/share/opensearch/config/jvm.options.d/synology.options && \
    echo "-Djava.awt.headless=true" >> /usr/share/opensearch/config/jvm.options.d/synology.options && \
    echo "-XX:+UseContainerSupport" >> /usr/share/opensearch/config/jvm.options.d/synology.options && \
    echo "-Dorg.opensearch.bootstrap.start_timeout=300s" >> /usr/share/opensearch/config/jvm.options.d/synology.options && \
    echo "-Dopensearch.logger.level=INFO" >> /usr/share/opensearch/config/jvm.options.d/synology.options && \
    echo "--add-opens=jdk.unsupported/sun.misc=ALL-UNNAMED" >> /usr/share/opensearch/config/jvm.options.d/synology.options && \
    echo "--add-opens=java.base/java.util=ALL-UNNAMED" >> /usr/share/opensearch/config/jvm.options.d/synology.options && \
    echo "--add-opens=java.base/java.lang=ALL-UNNAMED" >> /usr/share/opensearch/config/jvm.options.d/synology.options && \
    echo "--add-modules=jdk.unsupported" >> /usr/share/opensearch/config/jvm.options.d/synology.options && \
    echo "-XX:+UnlockExperimentalVMOptions" >> /usr/share/opensearch/config/jvm.options.d/synology.options && \
    echo "-XX:-UseVectorApi" >> /usr/share/opensearch/config/jvm.options.d/synology.options && \
    echo "-Djdk.incubator.vector.VECTOR_ACCESS_OOB_CHECK=0" >> /usr/share/opensearch/config/jvm.options.d/synology.options && \
    sed -i '/javaagent/d' /usr/share/opensearch/config/jvm.options && \
    echo '#!/bin/bash' > /usr/share/opensearch/start-opensearch.sh && \
    echo 'echo "Starting OpenSearch with Java 21..."' >> /usr/share/opensearch/start-opensearch.sh && \
    echo 'echo "Java version:"' >> /usr/share/opensearch/start-opensearch.sh && \
    echo 'java -version' >> /usr/share/opensearch/start-opensearch.sh && \
    echo 'echo "Memory info:"' >> /usr/share/opensearch/start-opensearch.sh && \
    echo 'free -h 2>/dev/null || echo "Memory info not available"' >> /usr/share/opensearch/start-opensearch.sh && \
    echo 'echo "Starting OpenSearch process..."' >> /usr/share/opensearch/start-opensearch.sh && \
    echo 'echo "Architecture info:"' >> /usr/share/opensearch/start-opensearch.sh && \
    echo 'uname -a' >> /usr/share/opensearch/start-opensearch.sh && \
    echo 'echo "CPU info:"' >> /usr/share/opensearch/start-opensearch.sh && \
    echo 'grep -E "^(processor|model name|flags)" /proc/cpuinfo | head -10 || echo "CPU info not available"' >> /usr/share/opensearch/start-opensearch.sh && \
    echo 'echo "Using JVM options file: /usr/share/opensearch/config/jvm.options.d/synology.options"' >> /usr/share/opensearch/start-opensearch.sh && \
    echo 'cat /usr/share/opensearch/config/jvm.options.d/synology.options' >> /usr/share/opensearch/start-opensearch.sh && \
    echo 'echo "Environment OPENSEARCH_JAVA_OPTS: $OPENSEARCH_JAVA_OPTS"' >> /usr/share/opensearch/start-opensearch.sh && \
    echo 'echo "Attempting to force disable vector operations..."' >> /usr/share/opensearch/start-opensearch.sh && \
    echo 'export OPENSEARCH_JAVA_OPTS="$OPENSEARCH_JAVA_OPTS -Dlucene.useVectorAPI=false -Dorg.apache.lucene.store.MMapDirectory.enableMemorySegments=false --limit-modules=java.base,java.logging,java.xml,java.management,java.naming,java.desktop,java.security.jgss,jdk.unsupported"' >> /usr/share/opensearch/start-opensearch.sh && \
    echo 'echo "Final OPENSEARCH_JAVA_OPTS: $OPENSEARCH_JAVA_OPTS"' >> /usr/share/opensearch/start-opensearch.sh && \
    echo 'echo "Starting OpenSearch binary..."' >> /usr/share/opensearch/start-opensearch.sh && \
    echo 'timeout 300s /usr/share/opensearch/bin/opensearch &' >> /usr/share/opensearch/start-opensearch.sh && \
    echo 'OPENSEARCH_PID=$!' >> /usr/share/opensearch/start-opensearch.sh && \
    echo 'echo "OpenSearch started with PID: $OPENSEARCH_PID"' >> /usr/share/opensearch/start-opensearch.sh && \
    echo 'wait $OPENSEARCH_PID' >> /usr/share/opensearch/start-opensearch.sh && \
    chmod +x /usr/share/opensearch/start-opensearch.sh && \
    chown -R opensearch:opensearch /usr/share/opensearch

USER opensearch

EXPOSE 9200 9300

# Use startup script for better debugging
ENTRYPOINT ["/usr/share/opensearch/start-opensearch.sh"]