"""
nox configuration file.
"""

import glob
import sys
from pathlib import Path

import nox

DIR = Path(__file__).parent.resolve()


@nox.session(venv_backend="none")
def pre_commit(session: nox.Session) -> None:
    """
    Run pre-commit
    """
    if "pre_commit" in sys.argv:
        session.run("pre-commit", "run", "--all-files")


@nox.session(venv_backend="none")
def dev(session: nox.Session) -> None:
    """
    Prepare a .venv folder.
    """

    library_output_dir = DIR / "python" / "myproj" / "simulation" / "_csrc"

    session.run(sys.executable, "-m", "venv", ".venv")
    session.run(
        ".venv/bin/pip",
        "install",
        "scikit-build-core[pyproject]",
        "pybind11",
        "pip>=23",
        "nox",
        "pytest",
        "pytest-cov",
        "pre-commit",
    )
    session.run(
        ".venv/bin/pip",
        "install",
        "--no-build-isolation",
        "--check-build-dependencies",
        "-ve",
        ".",
        "-Ccmake.define.CMAKE_EXPORT_COMPILE_COMMANDS=1",
        f"-Ccmake.define.CMAKE_LIBRARY_OUTPUT_DIRECTORY={library_output_dir}",
        "-Cbuild-dir=build",
    )


@nox.session(reuse_venv=True)
def cpp_lint_slow(session: nox.Session) -> None:
    if "cpp_lint_slow" in sys.argv:
        cpp_files = glob.glob("**/*.cc", recursive=True)
        session.run("clang-tidy", "-p", "build", *cpp_files)


@nox.session(reuse_venv=True)
def rr_run(session: nox.Session) -> None:
    """
    Run sp-repo-review.
    """

    session.install("sp-repo-review[cli]")
    session.run("repo-review", ".")


@nox.session
def rr_pylint(session: nox.Session) -> None:
    """
    Run PyLint.
    """
    # This needs to be installed into the package environment, and is slower
    # than a pre-commit check
    session.install("-e.")
    session.install("pylint>=3.2")
    session.run("pylint", "myproj.simulation", *session.posargs)