FROM pgvector/pgvector:pg15

# Install additional tools
RUN apt-get update && apt-get install -y \
    postgresql-client \
    cron \
    && rm -rf /var/lib/apt/lists/*

# Create directory for scripts
RUN mkdir -p /docker-entrypoint-initdb.d

# Copy initialization script
COPY init.sh /docker-entrypoint-initdb.d/
RUN chmod +x /docker-entrypoint-initdb.d/init.sh

# Copy refresh script
COPY refresh.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/refresh.sh

# Environment variables
ENV SOURCE_DB_URL=""
ENV TARGET_DB_NAME="shadow_db"
ENV TARGET_DB_USER="postgres"
ENV TARGET_DB_PASSWORD=""
ENV REFRESH_TIME="00:00"

# Define volume for persistent data
VOLUME ["/var/lib/postgresql/data"]

# Expose PostgreSQL port
EXPOSE 5432

# Add crontab file
RUN echo "0 0 * * * /usr/local/bin/refresh.sh >> /var/log/cron.log 2>&1" > /etc/cron.d/refresh-cron
RUN chmod 0644 /etc/cron.d/refresh-cron

# Create the log file to be able to run tail
RUN touch /var/log/cron.log

# Start cron and PostgreSQL
COPY docker-entrypoint.sh /
RUN chmod +x /docker-entrypoint.sh
ENTRYPOINT ["/docker-entrypoint.sh"]
