""" Explore data Structure api
"""

from core_explore_example_app.components.explore_data_structure.models import (
    ExploreDataStructure,
)
from core_explore_example_app.utils.parser import generate_form


def get_by_user_id_and_template_id(user_id, template_id):
    """Returns object with the given user id and template id

    Args:
        user_id:
        template_id:

    Returns:

    """
    return ExploreDataStructure.get_by_user_id_and_template_id(user_id, template_id)


def upsert(explore_data_structure):
    """Saves explore data structure

    Args:
        explore_data_structure:

    Returns:

    """
    explore_data_structure.save()
    return explore_data_structure


def create_and_get_explore_data_structure(template, request):
    """Get Data structure from template and user, generate them if no exist

    Args:
        template:
        request:

    Returns: Explore Data structure

    """
    try:
        # get data structure
        explore_data_structure = get_by_user_id_and_template_id(
            user_id=str(request.user.id), template_id=template.id
        )
    except:
        # create explore data structure
        explore_data_structure = ExploreDataStructure(
            user=str(request.user.id),
            template=template,
            name=template.filename,
        )
        upsert(explore_data_structure)
        # generate the root element
        root_element = generate_form(
            template.content, data_structure=explore_data_structure, request=request
        )
        explore_data_structure.data_structure_element_root = root_element
        upsert(explore_data_structure)

    # Return the data structure
    return explore_data_structure


def get_by_id(explore_data_structure_id):
    """Return the explore data structure with the given id

    Args:
        explore_data_structure_id:

    Returns:

    """
    return ExploreDataStructure.get_by_id(explore_data_structure_id)
