import tempfile
from typing import Any

import nox
from nox.sessions import Session

# nox.options.sessions = "lint", "safety", "vulture", "docs"
nox.options.sessions = "lint", "safety", "vulture", "vermin"

locations = ["graphysio"]
python_versions = ["3.9", "3.10", "3.11"]
vulture_whitelist = ".vulture_whitelist.py"


def install_with_constraints(session: Session, *args: str, **kwargs: Any) -> None:
    """Install packages constrained by Poetry's lock file.
    This function is a wrapper for nox.sessions.Session.install. It
    invokes pip to install packages inside of the session's virtualenv.
    Additionally, pip is passed a constraints file generated from
    Poetry's lock file, to ensure that the packages are pinned to the
    versions specified in poetry.lock. This allows you to manage the
    packages as Poetry development dependencies.
    Arguments:
        session: The Session object.
        args: Command-line arguments for pip.
        kwargs: Additional keyword arguments for Session.install.
    """
    with tempfile.NamedTemporaryFile() as requirements:
        session.run(
            "poetry",
            "export",
            "--with=dev",
            "--format=requirements.txt",
            "--without-hashes",
            f"--output={requirements.name}",
            external=True,
        )
        session.install(f"--constraint={requirements.name}", *args, **kwargs)


@nox.session(python=python_versions)
def lint(session):
    """Do the linting."""
    args = session.posargs or locations
    install_with_constraints(
        session,
        "flake8",
        "flake8-bandit",
        "flake8-bugbear",
    )
    session.run("flake8", *args)


@nox.session(python=python_versions)
def vulture(session):
    """Find dead code."""
    args = session.posargs or locations
    install_with_constraints(session, "vulture")
    session.run("vulture", "--exclude", ".vscode", vulture_whitelist, *args)


@nox.session(python=python_versions)
def safety(session: Session) -> None:
    """Scan dependencies for insecure packages."""
    with tempfile.NamedTemporaryFile() as requirements:
        session.run(
            "poetry",
            "export",
            "--with=dev",
            "--format=requirements.txt",
            "--without-hashes",
            f"--output={requirements.name}",
            external=True,
        )
        install_with_constraints(session, "safety")
        session.run("safety", "check", f"--file={requirements.name}", "--full-report")


@nox.session(python=python_versions)
def vermin(session):
    """Check minimum required Python version."""
    args = session.posargs or locations
    install_with_constraints(session, "vermin")
    session.run("vermin", "--no-tips", "--target=3.8-", *args)


@nox.session(python=python_versions)
def docs(session: Session) -> None:
    """Build the documentation."""
    session.run("poetry", "install", "--no-dev", external=True)
    install_with_constraints(session, "sphinx", "sphinx-autodoc-typehints")
    session.run("sphinx-build", "docs", "docs/_build")
