# Build stage
FROM rust:slim as builder

WORKDIR /usr/src/app

# Install dependencies required for building
RUN apt-get update && apt-get install -y pkg-config libssl-dev && rm -rf /var/lib/apt/lists/*

# Copy the manifests
COPY Cargo.toml Cargo.lock ./

# Create a dummy main.rs to build dependencies and cache them
RUN mkdir src && \
    echo "fn main() {}" > src/main.rs && \
    cargo build --release && \
    rm -rf src

# Copy the actual source code and migrations
COPY src ./src
COPY migrations ./migrations

# Touch main.rs to ensure cargo rebuilds it
RUN touch src/main.rs && cargo build --release

# Runtime stage
FROM debian:bookworm-slim

WORKDIR /app

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

# Copy the compiled binary from the builder stage
COPY --from=builder /usr/src/app/target/release/bootstrap-auth-server /usr/local/bin/bootstrap-auth-server

# Set environment variables
ENV SERVER_PORT=3000
ENV DATABASE_URL="sqlite://data.db?mode=rwc"
ENV RUST_LOG="bootstrap_auth_server=debug,info"

# Expose the port
EXPOSE 3000

# Run the binary
ENTRYPOINT ["bootstrap-auth-server"]
