from typing import Dict
import click


def validate_yml_environments(config: Dict, jobname=None) -> bool:
    """
    Validate the environment definitions (if defined)
    """
    # Do we have a global `environment` defined?
    environment = config.get("environment")
    if environment:
        if not isinstance(environment, dict):
            if jobname:
                click.echo(
                    f"Invalid definition of `environment` found in job `{jobname}`:\n"
                    f"environment: {environment}\n"
                    "\n"
                    "For environment documentation: https://docs.askanna.io/environments/",
                    err=True,
                )
            else:
                click.echo(
                    "Invalid definition of `environment` found:\n"
                    f"environment: {environment}\n"
                    "\n"
                    "For environment documentation: https://docs.askanna.io/environments/",
                    err=True,
                )
            return False
        else:
            # make sure we have at least the `image` defined
            image = environment.get("image")
            if not image:
                click.echo(
                    "`image` was not defined in the `environment`:\n"
                    f"environment: {environment}\n"
                    "\n"
                    "For environment documentation: https://docs.askanna.io/environments/",
                    err=True,
                )
                return False

    return True