import sys

def vetiver_write_docker(app_file: str = "app.py",
    path: str = ".",
    rspm_env: bool = False,
    host: str = "0.0.0.0",
    port: str = "80"):
    """Writes a Dockerfile to run VetiverAPI in a container

    Args
    ----
        app_file: str
            File containing VetiverAPI to be deployed into container
        path: str
            Path to save Dockerfile
        rspm_env: bool
            Whether or not RStudio Package Manager should be used
        host: str
            Host address to run VetiverAPI from Dockerfile
        port: str
            Port to run VetiverAPI from Dockerfile
    """
    py_version = str(sys.version_info.major) + "." + str(sys.version_info.minor)

    if rspm_env:
        rspm = "\nRUN pip config set global.index-url https://colorado.rstudio.com/rspm/pypi/latest/simple"
    else:
        rspm = ""


    docker_script = f"""# # Generated by the vetiver package; edit with care
#
FROM python:{py_version}

#
WORKDIR /code

#
COPY vetiver_requirements.txt /code/requirements.txt

#{rspm}
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt

#
COPY {app_file} /code/app

#
CMD ["uvicorn", "app.app:api", "--host", "{host}", "--port", "{port}"]
"""

    f = open(f"{path}Dockerfile", "x")
    f.write(docker_script)
