import demistomock as demisto  # noqa: F401
from CommonServerPython import *  # noqa: F401

ctx = demisto.context()
dataFromCtx = ctx.get("widgets")
if not dataFromCtx:
    incident = demisto.incidents()[0]
    accountName = incident.get('account')
    accountName = f"acc_{accountName}" if accountName != "" else ""

    stats = demisto.executeCommand(
        "demisto-api-post",
        {
            "uri": f"{accountName}/statistics/widgets/query",
            "body": {
                "size": 13,
                "dataType": "incidents",
                "query": "",
                "dateRange": {
                    "period": {
                        "byFrom": "months",
                        "fromValue": 12
                    }
                },
                "widgetType": "line",
                "params": {
                    "groupBy": [
                        "occurred(m)",
                        "null"
                    ],
                    "timeFrame": "months"
                },
            },
        })

    res = stats[0]["Contents"]["response"]

    buildNumber = demisto.executeCommand("DemistoVersion", {})[0]['Contents']['DemistoVersion']['buildNumber']
    buildNumber = f'{buildNumber}' if buildNumber != "REPLACE_THIS_WITH_CI_BUILD_NUM" else "618658"

    if int(buildNumber) >= 618657:
        # Line graph:
        data = {
            "Type": 17,
            "ContentsFormat": "line",
            "Contents": {
                "stats": res,
                "params": {
                    "timeFrame": "months"
                }
            }
        }

    else:
        # Bar graph:
        output = []
        for entry in res:
            output.append({"name": entry["name"], "data": entry["data"]})

        data = {
            "Type": 17,
            "ContentsFormat": "bar",
            "Contents": {
                "stats": output,
                "params": {
                    "layout": "horizontal"
                }
            }
        }

    demisto.results(data)
else:
    data = {
        "Type": 17,
        "ContentsFormat": "line",
        "Contents": {
            "stats": dataFromCtx['IncidentsCreatedMonthly'],
            "params": {
                "timeFrame": "months"
            }
        }
    }
    demisto.results(data)

[((4, 6, 4, 23), 'demistomock.context', 'demisto.context', ({}, {}), '()', True, 'import demistomock as demisto\n'), ((11, 12, 34, 10), 'demistomock.executeCommand', 'demisto.executeCommand', ({(12, 8, 12, 26): '"""demisto-api-post"""', (13, 8, 34, 9): "{'uri': f'{accountName}/statistics/widgets/query', 'body': {'size': 13,\n    'dataType': 'incidents', 'query': '', 'dateRange': {'period': {'byFrom':\n    'months', 'fromValue': 12}}, 'widgetType': 'line', 'params': {'groupBy':\n    ['occurred(m)', 'null'], 'timeFrame': 'months'}}}"}, {}), "('demisto-api-post', {'uri':\n    f'{accountName}/statistics/widgets/query', 'body': {'size': 13,\n    'dataType': 'incidents', 'query': '', 'dateRange': {'period': {'byFrom':\n    'months', 'fromValue': 12}}, 'widgetType': 'line', 'params': {'groupBy':\n    ['occurred(m)', 'null'], 'timeFrame': 'months'}}})", True, 'import demistomock as demisto\n'), ((71, 4, 71, 25), 'demistomock.results', 'demisto.results', ({(71, 20, 71, 24): 'data'}, {}), '(data)', True, 'import demistomock as demisto\n'), ((83, 4, 83, 25), 'demistomock.results', 'demisto.results', ({(83, 20, 83, 24): 'data'}, {}), '(data)', True, 'import demistomock as demisto\n'), ((7, 15, 7, 34), 'demistomock.incidents', 'demisto.incidents', ({}, {}), '()', True, 'import demistomock as demisto\n'), ((38, 18, 38, 62), 'demistomock.executeCommand', 'demisto.executeCommand', ({(38, 41, 38, 57): '"""DemistoVersion"""', (38, 59, 38, 61): '{}'}, {}), "('DemistoVersion', {})", True, 'import demistomock as demisto\n')]