# PostgreSQL 15 Alpine - Rolling tag for security updates
FROM postgres:15-alpine

# Set environment variables
ENV POSTGRES_DB=misinformation_db
ENV POSTGRES_USER=postgres
ENV POSTGRES_PASSWORD=postgres_dev_password_123
ENV PGDATA=/var/lib/postgresql/data/pgdata

# Install additional packages for production use
RUN apk add --no-cache \
    bash \
    curl \
    postgresql-client \
    && rm -rf /var/cache/apk/*

# Create necessary directories
RUN mkdir -p /docker-entrypoint-initdb.d \
    && mkdir -p /var/lib/postgresql/data \
    && mkdir -p /scripts \
    && mkdir -p /backups

# Copy initialization script
COPY init.sql /docker-entrypoint-initdb.d/01-init.sql

# Copy health check script
COPY health-check.sh /scripts/health-check.sh
RUN chmod +x /scripts/health-check.sh

# Set proper permissions
RUN chown -R postgres:postgres /var/lib/postgresql/data \
    && chown -R postgres:postgres /docker-entrypoint-initdb.d \
    && chown -R postgres:postgres /scripts \
    && chown -R postgres:postgres /backups

# PostgreSQL configuration for production
RUN echo "shared_preload_libraries = 'pg_stat_statements'" >> /usr/local/share/postgresql/postgresql.conf.sample \
    && echo "pg_stat_statements.track = all" >> /usr/local/share/postgresql/postgresql.conf.sample \
    && echo "log_statement = 'all'" >> /usr/local/share/postgresql/postgresql.conf.sample \
    && echo "log_duration = on" >> /usr/local/share/postgresql/postgresql.conf.sample \
    && echo "log_min_duration_statement = 100" >> /usr/local/share/postgresql/postgresql.conf.sample \
    && echo "shared_buffers = 256MB" >> /usr/local/share/postgresql/postgresql.conf.sample \
    && echo "effective_cache_size = 1GB" >> /usr/local/share/postgresql/postgresql.conf.sample \
    && echo "maintenance_work_mem = 64MB" >> /usr/local/share/postgresql/postgresql.conf.sample \
    && echo "checkpoint_completion_target = 0.9" >> /usr/local/share/postgresql/postgresql.conf.sample \
    && echo "wal_buffers = 16MB" >> /usr/local/share/postgresql/postgresql.conf.sample \
    && echo "default_statistics_target = 100" >> /usr/local/share/postgresql/postgresql.conf.sample \
    && echo "random_page_cost = 1.1" >> /usr/local/share/postgresql/postgresql.conf.sample \
    && echo "effective_io_concurrency = 200" >> /usr/local/share/postgresql/postgresql.conf.sample \
    && echo "work_mem = 4MB" >> /usr/local/share/postgresql/postgresql.conf.sample \
    && echo "min_wal_size = 1GB" >> /usr/local/share/postgresql/postgresql.conf.sample \
    && echo "max_wal_size = 4GB" >> /usr/local/share/postgresql/postgresql.conf.sample \
    && echo "max_worker_processes = 8" >> /usr/local/share/postgresql/postgresql.conf.sample \
    && echo "max_parallel_workers_per_gather = 4" >> /usr/local/share/postgresql/postgresql.conf.sample \
    && echo "max_parallel_workers = 8" >> /usr/local/share/postgresql/postgresql.conf.sample \
    && echo "max_parallel_maintenance_workers = 4" >> /usr/local/share/postgresql/postgresql.conf.sample

# Expose PostgreSQL port
EXPOSE 5432

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \
    CMD /scripts/health-check.sh || exit 1

# Use the postgres user
USER postgres

# Volume for data persistence
VOLUME ["/var/lib/postgresql/data", "/backups"]

# Start PostgreSQL
CMD ["postgres"]