# Use Python 3.11 slim image
FROM python:3.11-slim

# Set working directory
WORKDIR /app

# Copy shared directory from repo root context (required by pyproject.toml force-include)
COPY dmvf/shared /shared

# Install snowflake-data-validation from local source so the image always
# carries the latest local changes instead of the PyPI release.
COPY dmvf/snowflake-data-validation/pyproject.toml dmvf/snowflake-data-validation/README.md /sdv/
COPY dmvf/snowflake-data-validation/src/ /sdv/src/
RUN pip install --no-cache-dir /sdv

# Copy cloud-state-manager and install from the repo
COPY cloud-state-manager/pyproject.toml cloud-state-manager/README.md /cloud-state-manager/
COPY cloud-state-manager/python/ /cloud-state-manager/python/
RUN pip install --no-cache-dir -e /cloud-state-manager

# Copy project files
COPY dmvf/data-migration-orchestrator/pyproject.toml dmvf/data-migration-orchestrator/README.md ./
COPY dmvf/data-migration-orchestrator/src/ ./src/

# Install the package and dependencies
RUN pip install --no-cache-dir -e .
# TODO(SNOW-2860314): move dependency installation before the part in which we copy the source code to leverage Docker layer caching

# Set the Python path
ENV PYTHONPATH=/app/src

# Disable Python output buffering to see logs in real-time
ENV PYTHONUNBUFFERED=1

# Create non-root user and pre-create writable dirs.
# --create-home gives appuser a writable $HOME (/home/appuser); the scai logger
# (snowflake-code-unit-registry) derives its path from $HOME and creates
# ~/.snowflake/scai/logs at init — a --system user with no home crashes the
# process on the first log call (SNOW-3758069). .snowflake is pre-created for
# parity with the data-exchange-agent image.
RUN groupadd --system appgroup && useradd --system --gid appgroup --create-home appuser \
    && mkdir -p /app/logs && chown appuser:appgroup /app/logs \
    && mkdir -p /home/appuser/.snowflake && chown -R appuser:appgroup /home/appuser/.snowflake
USER appuser

# Run the main module
CMD ["python", "-u", "-m", "data_migration_orchestrator"]
