# syntax=docker/dockerfile:1.4
FROM python:3.11 AS builder

# Build arguments
ARG IS_FIRST_DEPLOYMENT="False"
# Add ARGs for variables needed by setup.py
ARG _PROJECT_ID="your-default-project-id-for-build"
ARG GCLOUD_PROJECT=$_PROJECT_ID
ARG _REGION="your-default-region-for-build"
ARG ENVIRONMENT="development"
ARG FRONTEND_URL=""
ARG BIG_QUERY_DATASET=""
ARG _CHATBOT_NAME=""
ARG _ADK_GOOGLE_PLACES_API_KEY=""
ARG GOOGLE_APPLICATION_CREDENTIALS=""

# Environment variables for the build process
ENV IS_FIRST_DEPLOYMENT=$IS_FIRST_DEPLOYMENT
ENV _PROJECT_ID=$_PROJECT_ID
ENV GCLOUD_PROJECT=$GCLOUD_PROJECT
ENV _REGION=$_REGION
ENV ENVIRONMENT=$ENVIRONMENT
ENV FRONTEND_URL=$FRONTEND_URL
ENV BIG_QUERY_DATASET=$BIG_QUERY_DATASET
ENV _CHATBOT_NAME=$_CHATBOT_NAME
ENV _ADK_GOOGLE_PLACES_API_KEY=$_ADK_GOOGLE_PLACES_API_KEY
ENV GOOGLE_APPLICATION_CREDENTIALS=$GOOGLE_APPLICATION_CREDENTIALS

WORKDIR /code

COPY . .

# --- Stage 1: Conditional First Deployment Setup ---
COPY prepare_code.sh ./prepare_code.sh
COPY setup.py ./setup.py

RUN --mount=type=secret,id=gcp_credentials,target=/tmp/gcp_adc.json,required=true \
    if [ "$IS_FIRST_DEPLOYMENT" = "True" ]; then \
        echo "--- Running first deployment setup (IS_FIRST_DEPLOYMENT=True) ---"; \
        echo "Using _PROJECT_ID=${_PROJECT_ID} and _REGION=${_REGION} for setup"; \
        \
        if [ ! -f /tmp/gcp_adc.json ]; then \
            echo "CRITICAL ERROR: GCP credentials secret was expected but not mounted to /tmp/gcp_adc.json" >&2; \
            exit 1; \
        fi; \
        echo "DEBUG: GCP credentials secret successfully mounted to /tmp/gcp_adc.json"; \
        \
        # Set GOOGLE_APPLICATION_CREDENTIALS specifically for this RUN command's execution context
        export GOOGLE_APPLICATION_CREDENTIALS=/tmp/gcp_adc.json; \
        echo "DEBUG: GOOGLE_APPLICATION_CREDENTIALS for setup is set to: $GOOGLE_APPLICATION_CREDENTIALS"; \
        \
        echo "Installing Poetry..."; \
        pip install poetry; \
        export PATH="$HOME/.local/bin:$PATH"; \
        poetry --version; \
        \
        echo "Executing prepare_code.sh..."; \
        chmod +x /code/prepare_code.sh; \
        bash /code/prepare_code.sh || { echo "prepare_code.sh failed!"; exit 1; }; \
        \
        echo "Executing setup.py with Poetry..."; \
        # Now setup.py can access _PROJECT_ID and _REGION as environment variables
        poetry run python /code/setup.py || { echo "setup.py with poetry failed!"; exit 1; }; \
        echo "--- First deployment setup finished ---"; \
    else \
        echo "--- IS_FIRST_DEPLOYMENT is False, skipping first deployment setup. ---"; \
    fi


FROM python:3.11-slim AS final

ARG _PROJECT_ID="your-default-project-id-for-build" 
ARG GCLOUD_PROJECT=$_PROJECT_ID
ARG _REGION="your-default-region-for-build"
ARG ENVIRONMENT="development"
ARG FRONTEND_URL=""
ARG BIG_QUERY_DATASET=""
ARG _CHATBOT_NAME=""
ARG _ADK_GOOGLE_PLACES_API_KEY=""
ARG GOOGLE_APPLICATION_CREDENTIALS=""

# Environment variables for the build process
ENV _PROJECT_ID=$_PROJECT_ID
ENV GCLOUD_PROJECT=$GCLOUD_PROJECT
ENV _REGION=$_REGION
ENV ENVIRONMENT=$ENVIRONMENT
ENV FRONTEND_URL=$FRONTEND_URL
ENV BIG_QUERY_DATASET=$BIG_QUERY_DATASET
ENV _CHATBOT_NAME=$_CHATBOT_NAME
ENV _ADK_GOOGLE_PLACES_API_KEY=$_ADK_GOOGLE_PLACES_API_KEY
ENV GOOGLE_APPLICATION_CREDENTIALS="/root/.config/gcloud/application_default_credentials.json"

WORKDIR /app

COPY --from=builder /code/requirements.txt /app/requirements.txt
RUN pip install --no-cache-dir -r /app/requirements.txt

COPY --from=builder /code/main.py /app/main.py
COPY --from=builder /code/src /app/src

EXPOSE 8080
ENTRYPOINT ["gunicorn", "main:app", "--workers=4", "--worker-class=uvicorn.workers.UvicornWorker", "--timeout=36000", "--bind=0.0.0.0:8080"]