# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

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

# Build arguments
ARG IS_FIRST_DEPLOYMENT="False"
ARG GCLOUD_PROJECT=""
ARG ENVIRONMENT="development"
ARG FRONTEND_URL=""
ARG FUNCTION_URL=""
ARG BIG_QUERY_DATASET=""

# Environment variables for the build process
ENV IS_FIRST_DEPLOYMENT=$IS_FIRST_DEPLOYMENT
ENV GCLOUD_PROJECT=$GCLOUD_PROJECT
ENV ENVIRONMENT=$ENVIRONMENT
ENV FRONTEND_URL=$FRONTEND_URL
ENV FUNCTION_URL=$FUNCTION_URL
ENV BIG_QUERY_DATASET=$BIG_QUERY_DATASET

WORKDIR /code

COPY . .
COPY setup.py ./setup.py
RUN pip install --no-cache-dir -r /code/requirements.txt

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 GCLOUD_PROJECT=${GCLOUD_PROJECT} 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 "Executing setup.py ..."; \
        # Now setup.py can make requests to GCP with provided credentials
        python /code/setup.py || { echo "setup.py with 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 GCLOUD_PROJECT=""
ARG GOOGLE_APPLICATION_CREDENTIALS=""
ARG ENVIRONMENT="development"
ARG FRONTEND_URL=""
ARG FUNCTION_URL=""
ARG BIG_QUERY_DATASET=""

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

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"]