# Path: pytest_bdd/generation.py
# def generate_code(features: list[Feature], scenarios: list[ScenarioTemplate], steps: list[Step]) -> str:
#     """Generate test code for the given filenames."""
#     grouped_steps = group_steps(steps)
#     template = template_lookup.get_template("test.py.mak")
#     code = template.render(
#         features=features,
#         scenarios=scenarios,
#         steps=grouped_steps,
#         make_python_name=make_python_name,
#         make_python_docstring=make_python_docstring,
#         make_string_literal=make_string_literal,
#     )
#     return cast(str, code)
# 
# def parse_feature_files(paths: list[str], **kwargs: Any) -> tuple[list[Feature], list[ScenarioTemplate], list[Step]]:
#     """Parse feature files of given paths.
# 
#     :param paths: `list` of paths (file or dirs)
# 
#     :return: `list` of `tuple` in form:
#              (`list` of `Feature` objects, `list` of `Scenario` objects, `list` of `Step` objects).
#     """
#     features = get_features(paths, **kwargs)
#     scenarios = sorted(
#         itertools.chain.from_iterable(feature.scenarios.values() for feature in features),
#         key=lambda scenario: (scenario.feature.name or scenario.feature.filename, scenario.name),
#     )
#     steps = sorted(
#         set(itertools.chain.from_iterable(scenario.steps for scenario in scenarios)), key=lambda step: step.name
#     )
#     return features, scenarios, steps


import argparse
import os.path
import re
import glob2
from .generation import generate_code, parse_feature_files


def migrate_tests_in_file(file_path: str) -> None:
    """Migrate all bdd-based tests in the given test file."""
    try:
        with open(file_path, "r+") as fd:
            content = fd.read()
            new_content = MIGRATE_REGEX.sub(r"\n@scenario(\2)\ndef \1():\n    pass\n", content)
            if new_content != content:
                # the regex above potentially causes the end of the file to
                # have an extra newline
                new_content = new_content.rstrip("\n") + "\n"
                fd.seek(0)
                fd.write(new_content)
                print(f"migrated: {file_path}")
            else:
                print(f"skipped: {file_path}")
    except OSError:
        pass


def check_existense(file_name: str) -> str:
    """Check file or directory name for existence."""
    if not os.path.exists(file_name):
        raise argparse.ArgumentTypeError(f"{file_name} is an invalid file or directory name")
    return file_name


def print_generated_code(args: argparse.Namespace) -> None:
    """Print generated test code for the given filenames."""

# Path: pytest_bdd/generation.py
# def generate_code(features: list[Feature], scenarios: list[ScenarioTemplate], steps: list[Step]) -> str:
#     """Generate test code for the given filenames."""
#     grouped_steps = group_steps(steps)
#     template = template_lookup.get_template("test.py.mak")
#     code = template.render(
#         features=features,
#         scenarios=scenarios,
#         steps=grouped_steps,
#         make_python_name=make_python_name,
#         make_python_docstring=make_python_docstring,
#         make_string_literal=make_string_literal,
#     )
#     return cast(str, code)
# 
# def parse_feature_files(paths: list[str], **kwargs: Any) -> tuple[list[Feature], list[ScenarioTemplate], list[Step]]:
#     """Parse feature files of given paths.
# 
#     :param paths: `list` of paths (file or dirs)
# 
#     :return: `list` of `tuple` in form:
#              (`list` of `Feature` objects, `list` of `Scenario` objects, `list` of `Step` objects).
#     """
#     features = get_features(paths, **kwargs)
#     scenarios = sorted(
#         itertools.chain.from_iterable(feature.scenarios.values() for feature in features),
#         key=lambda scenario: (scenario.feature.name or scenario.feature.filename, scenario.name),
#     )
#     steps = sorted(
#         set(itertools.chain.from_iterable(scenario.steps for scenario in scenarios)), key=lambda step: step.name
#     )
#     return features, scenarios, steps


# Path: pytest_bdd/scripts.py
import argparse
import os.path
import re
import glob2
from .generation import generate_code, parse_feature_files



def migrate_tests_in_file(file_path: str) -> None:
    """Migrate all bdd-based tests in the given test file."""
    try:
        with open(file_path, "r+") as fd:
            content = fd.read()
            new_content = MIGRATE_REGEX.sub(r"\n@scenario(\2)\ndef \1():\n    pass\n", content)
            if new_content != content:
                # the regex above potentially causes the end of the file to
                # have an extra newline
                new_content = new_content.rstrip("\n") + "\n"
                fd.seek(0)
                fd.write(new_content)
                print(f"migrated: {file_path}")
            else:
                print(f"skipped: {file_path}")
    except OSError:
        pass


def check_existense(file_name: str) -> str:
    """Check file or directory name for existence."""
    if not os.path.exists(file_name):
        raise argparse.ArgumentTypeError(f"{file_name} is an invalid file or directory name")
    return file_name


def print_generated_code(args: argparse.Namespace) -> None:
    """Print generated test code for the given filenames."""

    features, scenarios, steps = parse_feature_files(args.files)