FROM solr:9.9.0

# Switch to root to set up configuration
USER root

# Copy Solr configurations into the image
COPY ./solr/stories /opt/solr-9.9.0/server/solr/configsets/storycove_stories
COPY ./solr/authors /opt/solr-9.9.0/server/solr/configsets/storycove_authors

# Create initialization script using the precreate-core pattern
COPY <<EOF /docker-entrypoint-initdb.d/init-cores.sh
#!/bin/bash
echo "StoryCove: Initializing cores..."

# Use solr's built-in precreate-core functionality
precreate-core storycove_stories /opt/solr-9.9.0/server/solr/configsets/storycove_stories
precreate-core storycove_authors /opt/solr-9.9.0/server/solr/configsets/storycove_authors

echo "StoryCove: Core initialization complete!"
EOF

# Ensure proper permissions and make script executable
RUN chown -R solr:solr /opt/solr-9.9.0/server/solr/configsets/ && \
    chmod +x /docker-entrypoint-initdb.d/init-cores.sh && \
    chown solr:solr /docker-entrypoint-initdb.d/init-cores.sh

# Switch back to solr user
USER solr

# Use the default Solr entrypoint
CMD ["solr-foreground"]