import sys
import warnings


def vetiver_write_docker(
    app_file: str = "app.py",
    path: str = "./",
    rspm_env: bool = False,
    host: str = "0.0.0.0",
    port: str = "8080",
):

    warnings.warn(
        "vetiver_write_docker will be replaced by write_docker in v1.0.0",
        DeprecationWarning,
    )

    return write_docker(
        app_file=app_file, path=path, rspm_env=rspm_env, host=host, port=port
    )


def write_docker(
    app_file: str = "app.py",
    path: str = "./",
    rspm_env: bool = False,
    host: str = "0.0.0.0",
    port: str = "8080",
):
    """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"  # noqa
    else:
        rspm = ""

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

# create directory in container for vetiver files
WORKDIR /vetiver

# copy  and install requirements
COPY vetiver_requirements.txt /vetiver/requirements.txt

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

# copy app file
COPY {app_file} /vetiver/app

# expose port
EXPOSE {port}

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

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