"""
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.
    """

    session.run(sys.executable, "-m", "venv", ".venv")
    session.run(
        ".venv/bin/pip",
        "install",
        "scikit-build-core[pyproject]",
        "pybind11",
        "pip>=23",
    )
    session.run(
        ".venv/bin/pip",
        "install",
        "--no-build-isolation",
        "--check-build-dependencies",
        "-ve.",
        "-C",
        "cmake.define.CMAKE_EXPORT_COMPILE_COMMANDS=1",
        "-C",
        "build-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 cpp_lint(session: nox.Session) -> None:
    cpp_files = glob.glob("**/*.cc", recursive=True)
    session.install("clangd-tidy")
    session.run("clangd-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_lint(session: nox.Session) -> None:
#     """
#     Run the linter.
#     """
#     session.install("pre-commit")
#     session.run("pre-commit", "run", "--all-files", *session.posargs)


@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)


# @nox.session
# def rr_tests(session: nox.Session) -> None:
#     """
#     Run the unit and regular tests for sp-repo-review.
#     """
#     session.install("-e.[test,cli]")
#     session.run("pytest", *session.posargs, env={"PYTHONWARNDEFAULTENCODING": "1"})


# @nox.session(reuse_venv=True)
# def rr_build(session: nox.Session) -> None:
#     """
#     Build an SDist and wheel for sp-repo-review.
#     """

#     build_p = DIR.joinpath("build")
#     if build_p.exists():
#         shutil.rmtree(build_p)

#     session.install("build")
#     session.run("python", "-m", "build")


@nox.session
def build_extension(session: nox.Session) -> None:
    # Install build dependencies
    session.install(
        "pybind11", "scikit-build", "setuptools", "wheel", "pybind11-stubgen"
    )

    # Build the extension module using scikit-build
    session.run("python", "-m", "pip", "install", ".", "--no-build-isolation")

    # Generate the .pyi file using pybind11-stubgen
    # Replace 'your_package._hello' with the actual module name
    session.run("pybind11-stubgen", "your_package._hello")

    # Move the generated .pyi file to the source directory
    # By default, pybind11-stubgen outputs to 'stubs/your_package/_hello/__init__.pyi'
    stub_src = (
        Path("stubs") / "myproj" / "simulation" / "_csrc" / "_hello" / "__init__.pyi"
    )
    stub_dest = Path("myproj") / "simulation" / "_csrc" / "_hello.pyi"
    session.run("mv", stub_src, stub_dest, external=True)

    # Clean up the stubs directory
    session.run("rm", "-r", "stubs", external=True)