# Dockerfile
FROM 086679231553.dkr.ecr.us-east-1.amazonaws.com/docker-parent-images:python311 AS base
WORKDIR     /var/app

# Update and install common packages
USER        root
RUN         apt update -y && \
                apt install -y gcc libffi-dev zip && \
                apt-get clean

# Switch to the app directory
WORKDIR     /var/app
COPY        --from=ghcr.io/astral-sh/uv:0.7.18 /uv /uvx /bin/
COPY        pyproject.toml uv.lock ./
RUN         touch README.md && \
                uv sync --all-extras

COPY        ./pdp ./pdp

# Set ownership of the .pdm-build and .venv directories to the appuser
RUN         chown -R worker:worker /var/app/.pdm-build || true && \
                chown -R worker:worker /var/app/.venv || true


#######################################
### Build image for live deployment ###
#######################################
FROM        base AS deploy
USER        root
RUN         apt-get purge gcc libffi-dev zip -y && \
                # Remove the CLI module to avoid changing DDB outside of terraform
                rm -rf ./pdp/cli

# Add uvicorn script for fastapi
COPY        uvicorn-start.sh  /
RUN         chmod +x /uvicorn-start.sh

EXPOSE      8080

USER        worker

ENTRYPOINT  ["/uvicorn-start.sh"]


#######################################
### Build image for CLI tools       ###
#######################################
FROM        base AS pdpcli

USER        root

COPY        ./pdp_qa_refresh  ./pdp_qa_refresh

RUN         uv sync

USER        worker
ENTRYPOINT  ["uv", "run", "pdpcli"]


###########################
### Build image for dev ###
###########################
FROM        base AS dev
USER        root

COPY        dev.py ./
COPY        ./tests ./tests
COPY        ./scripts ./scripts

RUN         uv sync --group dev --all-extras

EXPOSE      5000
USER        worker
CMD         ["uv", "run", "ddtrace-run", "python", "dev.py", "2>&1"]


#################################
### Build image for unit-lint ###
#################################
FROM        dev AS unit-lint

# Set ownership of the .pdm-build and .venv directories to the appuser
RUN         chown -R worker:worker /var/app/.pdm-build && \
                chown -R worker:worker /var/app/.venv

USER        worker
ENTRYPOINT  ["scripts/unit-lint.sh"]


########################################
### Build image for integration-test ###
########################################
FROM        dev AS integration-test
USER        root
RUN         uv sync --group integration --all-extras

# change to the app dir.
WORKDIR     /var/app

USER        worker
ENTRYPOINT  ["scripts/integration.sh"]


#################################
### Build image for start-db  ###
#################################
FROM        dev AS start-db

USER        worker
ENTRYPOINT  ["scripts/start-db.sh"]
