# Build stage
FROM rust:1.75 as builder

WORKDIR /app

# Copy manifest files
COPY Cargo.toml Cargo.lock ./

# Copy source code
COPY src ./src

# Build the application in release mode
RUN cargo build --release

# Runtime stage
FROM debian:bookworm-slim

# Install necessary runtime dependencies
RUN apt-get update && apt-get install -y \
    ca-certificates \
    libssl3 \
    && rm -rf /var/lib/apt/lists/*

# Create a non-root user
RUN useradd -r -s /bin/false pgpeek

# Copy the built binary from builder stage
COPY --from=builder /app/target/release/pgpeek /usr/local/bin/pgpeek

# Create config directory
RUN mkdir -p /home/pgpeek/.config/pgpeek && \
    chown -R pgpeek:pgpeek /home/pgpeek

# Switch to non-root user
USER pgpeek
WORKDIR /home/pgpeek

# Set environment variable to ensure proper terminal handling
ENV TERM=xterm-256color

# Default command
ENTRYPOINT ["pgpeek"]
CMD ["--help"]