import pathlib
import datetime
import warnings


def node_stub(
    node_name: str,
    node_type: str,
    node_class: str | None = None,
    author: str = '',
    email: str = '',
    destination_folder: str = './plugins',
):
    """
    Create a basic node template with a config file and a README file.
    The template is created in the specified destination folder.

    Parameters
    ----------
    node_name : str
        The name of the node. This is the name of the folder that will be
        created in the destination folder. Do not prefix the name with a
        underscore, as this will be done automatically.
    node_type : str
        The type of the node.
    node_class : str
        The name of the class that will be created in the node file. If not
        provided, the class name will be generated by capitalising the first
        letter of each word in the node name, splitting by underscores.
    author : str
        Name of the node author.
    email : str
        Email address of the corresponding node author.
    destination_folder : str
        The destination folder where the node template will be created. The
        default is './plugins'.

    """
    node_destination = pathlib.Path(
        destination_folder, node_type, f'_{node_name}'
    )

    if node_destination.exists():
        warnings.warn(
            f'Node {node_name} already exists in {node_destination}. '
            'Please choose a different name or delete the existing node.',
            UserWarning,
            stacklevel=1,
        )

        return

    node_destination.mkdir(parents=True)
    base_path = pathlib.Path(__file__).parent

    template_path = base_path / 'basic_node.template'
    config_path = base_path / 'basic_config.template'

    with open(template_path) as template_file:
        template = template_file.read()

    if node_class is None:
        node_class = ''.join(x.capitalize() for x in node_name.split('_'))

    template = template.replace('$_node_class_name', node_class)
    template = template.replace('$_node_author_name', author)
    template = template.replace('$_node_author_email', email)
    template = template.replace(
        '$_creation_datetime',
        datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
    )

    node_file_path = pathlib.Path(node_destination, f'{node_name}.py')

    with open(node_file_path, 'w') as node_file:
        node_file.write(template)

    with open(config_path) as config_content:
        config = config_content.read()

    with open(pathlib.Path(node_destination, 'config.toml'), 'w') as cfg_file:
        cfg_file.write(config)

    with open(pathlib.Path(node_destination, 'README.md'), 'w') as readme_file:
        readme_file.write(
            f'# {node_name}\n\n'
            f'## Node type: {node_type}\n\n'
            f'## Node class name: {node_class}\n\n'
            f'## Node name: {node_name}\n\n'
        )

    open(pathlib.Path(node_destination, 'requirements.txt'), 'a').close()

    print('Node creation successful!')
