# Path: src/tests/behave/api/features/common/utils.py
# def get_entity_list_container_field(name):
#     """Returns the container field used in list responses
# 
#     GET /active_computers       -> {"items": [...]}
#     GET /jobs                   -> {"jobs": [...]}
#     """
#     if name == "active_computers":
#         return "items"
#     elif name == "inventory_computers":
#         return "items"
#     elif name == "rpc_tasks":
#         return "tasks"
#     elif name == "rpc_jobs":
#         return "jobs"
#     return name
# 
# def get_entity_id_field(name):
#     """Returns the id field used for entities in list responses
# 
#     GET /active_computers       -> {"items": [{"mercury_id": "...", ...}}]}
#     GET /jobs                   -> {"jobs": [{"job_id": "...", ...}}]}
#     """
#     if name == "active_computers":
#         return "mercury_id"
#     elif name == "inventory_computers":
#         return "mercury_id"
#     elif name == "rpc_tasks":
#         return "task_id"
#     elif name == "rpc_jobs":
#         return "job_id"
#     return name
# 
# def read_json_from_file(filename, location):
#     filename = "{0}/{1}".format(location, filename)
#     with open(filename) as file:
#         data = json.loads(file.read())
#     return data


import json
import random
from behave import when, step
from src.tests.behave.api.features.common.utils import (
    get_entity_list_container_field,
    get_entity_id_field,
    read_json_from_file,
)
        # TODO if this is empty add one somehow?
        context.check.assertGreater(len(service_entities), 0)
        i = random.randint(0, len(service_entities) - 1)
        entity_id = service_entities[i][field_name]
        context.services[service_name]["id"] = entity_id
    except IndexError:
        context.check.assertIsNotNone(
            context.services[service_name]["id"],
            msg="WIP: Create {} API Not Yet Implemented".format(service_name),
        )
        # TODO: Create new service entity since we don't have any


@step(
    "first device queried from {filename} of type {service_name} entity id is located for testing"
)
def step_a_service_id_is_located_for_testing(context, service_name, filename):
    """
    :type context: behave.runner.Context
    :type filename: str
    :type service_name: str
    """

    location = context.json_location
    data = read_json_from_file(filename, location)

    service_client = context.services[service_name]["client"]

    response = service_client.post(data=json.dumps(data), url_suffix="query")


# Path: src/tests/behave/api/features/common/utils.py
# def get_entity_list_container_field(name):
#     """Returns the container field used in list responses
# 
#     GET /active_computers       -> {"items": [...]}
#     GET /jobs                   -> {"jobs": [...]}
#     """
#     if name == "active_computers":
#         return "items"
#     elif name == "inventory_computers":
#         return "items"
#     elif name == "rpc_tasks":
#         return "tasks"
#     elif name == "rpc_jobs":
#         return "jobs"
#     return name
# 
# def get_entity_id_field(name):
#     """Returns the id field used for entities in list responses
# 
#     GET /active_computers       -> {"items": [{"mercury_id": "...", ...}}]}
#     GET /jobs                   -> {"jobs": [{"job_id": "...", ...}}]}
#     """
#     if name == "active_computers":
#         return "mercury_id"
#     elif name == "inventory_computers":
#         return "mercury_id"
#     elif name == "rpc_tasks":
#         return "task_id"
#     elif name == "rpc_jobs":
#         return "job_id"
#     return name
# 
# def read_json_from_file(filename, location):
#     filename = "{0}/{1}".format(location, filename)
#     with open(filename) as file:
#         data = json.loads(file.read())
#     return data


# Path: src/tests/behave/api/features/steps/details.py
import json
import random
from behave import when, step
from src.tests.behave.api.features.common.utils import (
    get_entity_list_container_field,
    get_entity_id_field,
    read_json_from_file,
)

        # TODO if this is empty add one somehow?
        context.check.assertGreater(len(service_entities), 0)
        i = random.randint(0, len(service_entities) - 1)
        entity_id = service_entities[i][field_name]
        context.services[service_name]["id"] = entity_id
    except IndexError:
        context.check.assertIsNotNone(
            context.services[service_name]["id"],
            msg="WIP: Create {} API Not Yet Implemented".format(service_name),
        )
        # TODO: Create new service entity since we don't have any


@step(
    "first device queried from {filename} of type {service_name} entity id is located for testing"
)
def step_a_service_id_is_located_for_testing(context, service_name, filename):
    """
    :type context: behave.runner.Context
    :type filename: str
    :type service_name: str
    """

    location = context.json_location
    data = read_json_from_file(filename, location)

    service_client = context.services[service_name]["client"]

    response = service_client.post(data=json.dumps(data), url_suffix="query")


    container_field = get_entity_list_container_field(service_name)