from flask import render_template, current_app


def get_list_view_config(title, data, columns, actions, initial_sort=None, table_id=None, additional_config=None):
    """
    Creates a standardized configuration dictionary for list views.

    Args:
        title (str): The title of the page
        data (list): The data to display in the table
        columns (list): Column definitions for the table
        actions (list): Action button definitions
        initial_sort (list, optional): Initial sort configuration
        table_id (str, optional): Custom table ID, generated from title if not provided
        additional_config (dict, optional): Any additional configuration to include

    Returns:
        dict: A standardized configuration dictionary
    """
    # Generate table_id from title if not provided
    if not table_id:
        table_id = f"{title.lower().replace(' ', '_')}_table"

    # Create the base configuration
    config = {
        'title': title,
        'data': data,  # Consistent data parameter name
        'columns': columns,
        'actions': actions,
        'initial_sort': initial_sort or [],
        'table_id': table_id
    }

    # Add any additional configuration
    if additional_config:
        config.update(additional_config)

    return config

def render_list_view(template_name, title, data, columns, actions, form_action, initial_sort=None,
                     table_id=None, additional_config=None, **kwargs):
    """
    Renders a list view template with standardized configuration.

    Args:
        template_name (str): The name of the template to render
        title (str): The title of the page
        data (list): The data to display in the table
        columns (list): Column definitions for the table
        actions (list): Action button definitions
        form_action (str): Form action URL for the table
        initial_sort (list, optional): Initial sort configuration
        table_id (str, optional): Custom table ID
        additional_config (dict, optional): Any additional configuration
        **kwargs: Additional template variables

    Returns:
        str: The rendered template
    """
    # Zorg ervoor dat table_id altijd een string is zonder spaties of speciale tekens
    if not table_id:
        table_id = f"{title.lower().replace(' ', '_').replace('-', '_')}_table"

    # Zorg ervoor dat initial_sort altijd een lijst is
    if initial_sort is None:
        initial_sort = []

    # Zorg ervoor dat actions altijd een lijst is
    if actions is None:
        actions = []

    # Maak config dictionary
    config = {
        'title': title,
        'data': data,
        'columns': columns,
        'actions': actions,
        'initial_sort': initial_sort,
        'table_id': table_id,
        'form_action': form_action
    }

    # Voeg extra configuratie toe indien aanwezig
    if additional_config:
        config.update(additional_config)

    # Voeg eventuele extra template variabelen toe
    config.update(kwargs)

    current_app.logger.debug(f"List view config: {config}")

    return render_template(template_name, **config)
