# Unified Codeflash Container - Using Pre-built Images
FROM node:20-alpine

# Install Python, PostgreSQL, and supervisor
RUN apk add --no-cache \
    python3 \
    py3-pip \
    postgresql15 \
    postgresql15-client \
    supervisor \
    bash \
    openssl \
    && rm -rf /var/cache/apk/*

# Install Python uv
RUN pip3 install --break-system-packages uv

WORKDIR /app

# Copy cf-api from pre-built image
COPY --from=codeflash/cf-api:latest /app /app/cf-api
COPY --from=codeflash/cf-api:latest /common /common

# Copy aiservice from pre-built image
COPY --from=codeflash/aiservice:latest /app /app/aiservice

# Create PostgreSQL data directory with correct permissions
RUN mkdir -p /var/lib/postgresql/data /var/run/postgresql && \
    chown -R postgres:postgres /var/lib/postgresql /var/run/postgresql

# Copy configuration files
COPY deployment/onprem-simple/supervisord.conf /etc/supervisord.conf
COPY deployment/onprem-simple/init-db.sh /app/init-db.sh
COPY deployment/onprem-simple/startup.sh /app/startup.sh

RUN chmod +x /app/init-db.sh /app/startup.sh

# Expose ports
EXPOSE 5432 8000 3001

# Set environment variables
ENV PGDATA=/var/lib/postgresql/data
ENV PATH="/var/lib/postgresql/bin:${PATH}"

# Start supervisor
CMD ["/app/startup.sh"]