"""
Autogenerated state module using `pop-create-idem <https://gitlab.com/saltstack/pop/pop-create-idem>`__

hub.exec.boto3.client.securityhub.create_insight
hub.exec.boto3.client.securityhub.delete_insight
hub.exec.boto3.client.securityhub.get_insights
hub.exec.boto3.client.securityhub.update_insight
"""



from typing import *
import dict_tools.differ as differ
async def present(hub, ctx, name: Text, filters: Dict, group_by_attribute: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Creates a custom insight in Security Hub. An insight is a consolidation of findings that relate to a security
    issue that requires attention or remediation. To group the related findings in the insight, use the
    GroupByAttribute.

    Args:
        name(Text): The name of the custom insight to create.
        filters(Dict): One or more attributes used to filter the findings included in the insight. The insight only
            includes findings that match the criteria defined in the filters.
        group_by_attribute(Text): The attribute used to group the findings for the insight. The grouping attribute identifies the
            type of item that the insight applies to. For example, if an insight is grouped by resource
            identifier, then the insight produces a list of resource identifiers.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_present:
              aws_auto.securityhub.insight.present:
                - name: value
                - filters: value
                - group_by_attribute: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.securityhub.insight.id(
        ctx,
        jmes_path=name
    )
    if ret["status"]:
        # name is now the first id that matched the JMES search path
        name = ret["ret"]
    

    
    before = await hub.exec.boto3.client.securityhub.get_insights(name)

    if before:
        result["comment"] = f"'{name}' already exists"
    else:
        try:
            ret = await hub.exec.boto3.client.securityhub.create_insight(
                ctx,
                
                
                **{"Name": name, "Filters": filters, "GroupByAttribute": group_by_attribute}
            )
            result["result"] = ret["status"]
            if not result["result"]:
                result["comment"] = ret["comment"]
                return result
            ret["comment"] = f"Created '{name}'"
        except hub.tool.boto3.exception.ClientError as e:
            result["comment"] = f"{e.__class__.__name__}: {e}"

    
    # TODO perform other modifications as needed here
    ...

    after = await hub.exec.boto3.client.securityhub.get_insights(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

async def absent(hub, ctx, name: Text, insight_arn: Text)  -> Dict[str, Any]:
    r'''
    **Autogenerated function**
    
    Deletes the insight specified by the InsightArn.

    Args:
        name(Text): A name, ID, or JMES search path to identify the resource.
        insight_arn(Text): The ARN of the insight to delete.

    Returns:
        Dict[str, Any]

    Examples:

        .. code-block:: sls

            resource_is_absent:
              aws_auto.securityhub.insight.absent:
                - name: value
                - insight_arn: value
    '''
    
    result = dict(comment="", changes= None, name=name, result=True)
    ret = await hub.exec.boto3.client.securityhub.insight.id(
        ctx,
        jmes_path=name
    )
    if ret["status"]:
        # name is now the first id that matched the JMES search path
        name = ret["ret"]
    

    

    before = await hub.exec.boto3.client.securityhub.get_insights(name)

    if not before:
        result["comment"] = f"'{name}' already absent"
    else:
        try:
            ret = await hub.exec.boto3.client.securityhub.delete_insight(
                ctx,
                
                
                **{"InsightArn": insight_arn}
            )
            result["result"] = ret["status"]
            if not result["result"]:
                result["comment"] = ret["comment"]
                return result
            result["comment"] = f"Deleted '{name}'"
        except hub.tool.boto3.exception.ClientError as e:
            result["comment"] = f"{e.__class__.__name__}: {e}"

    

    after = await hub.exec.boto3.client.securityhub.get_insights(name)
    result["changes"] = differ.deep_diff(before, after)
    return result

