# Use official Python runtime as a parent image
FROM python:3.9-slim

# Set the working directory in the container
WORKDIR /app

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Install system dependencies including PostgreSQL client and netcat
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    libpq-dev \
    postgresql-client \
    curl \
    netcat-openbsd \
    && rm -rf /var/lib/apt/lists/*

# Install Python dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy the current directory contents into the container at /app
COPY . .

# Copy and set up startup scripts
COPY start.sh /app/start.sh
COPY docker-start.sh /app/docker-start.sh
RUN chmod +x /app/start.sh /app/docker-start.sh

# Expose the port the app runs on
EXPOSE 8002

# Run Docker-optimized startup script
CMD ["/app/docker-start.sh"]