# Copyright (c) 2020 Nekokatt
# Copyright (c) 2021-present davfsa
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from __future__ import annotations

import pathlib

from pipelines import config
from pipelines import nox

STUBGEN_GENERATE = [
    "hikari/__init__.py",
    "hikari/api/__init__.py",
    "hikari/events/__init__.py",
    "hikari/impl/__init__.py",
    "hikari/interactions/__init__.py",
]


@nox.session(requires=["generate-stubs"])
def mypy(session: nox.Session) -> None:
    """Perform static type analysis on Python source code using mypy."""
    nox.sync(session, self=True, extras=["speedups", "server"], groups=["mypy"])

    session.run("mypy", "-p", config.MAIN_PACKAGE, "--config", config.PYPROJECT_TOML)
    session.run("mypy", "-p", config.EXAMPLE_SCRIPTS, "--config", config.PYPROJECT_TOML)


@nox.session()
def generate_stubs(session: nox.Session) -> None:
    """Generate the stubs for the package."""
    nox.sync(session, groups=["mypy", "ruff"])

    session.run("stubgen", *STUBGEN_GENERATE, "-o", ".", "--include-private", "--no-import")

    stub_paths = [path + "i" for path in STUBGEN_GENERATE]

    # At the time of writing, sorting imports is not done when running formatting
    # and needs to be done with ruff check
    # see: https://docs.astral.sh/ruff/formatter/#sorting-imports
    session.run("ruff", "format", *stub_paths)
    session.run("ruff", "check", "--select", "I", "--fix", *stub_paths)

    for stub_path in stub_paths:
        path = pathlib.Path(stub_path)

        content = path.read_bytes()

        path.write_text(
            "# DO NOT MANUALLY EDIT THIS FILE!\n"
            "# This file was automatically generated by `nox -s generate-stubs`\n\n" + content.decode()
        )
