# 1. Build Stage
FROM rust:1.90 as builder

WORKDIR /usr/src/app

# Pre-build dependencies to leverage Docker layer caching
COPY ./services/common-contracts /usr/src/app/services/common-contracts
COPY ./services/finnhub-provider-service/Cargo.toml ./services/finnhub-provider-service/Cargo.lock* ./services/finnhub-provider-service/

WORKDIR /usr/src/app/services/finnhub-provider-service
RUN mkdir -p src && \
    echo "fn main() {}" > src/main.rs && \
    cargo build --release --bin finnhub-provider-service

# Copy the full source code
COPY ./services/finnhub-provider-service /usr/src/app/services/finnhub-provider-service

# Build the application
WORKDIR /usr/src/app/services/finnhub-provider-service
RUN cargo build --release --bin finnhub-provider-service

# 2. Runtime Stage
FROM debian:bookworm-slim

# Set timezone
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# Copy the built binary from the builder stage
COPY --from=builder /usr/src/app/services/finnhub-provider-service/target/release/finnhub-provider-service /usr/local/bin/

# Set the binary as the entrypoint
ENTRYPOINT ["/usr/local/bin/finnhub-provider-service"]
