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)

[((98, 115), 'demistomock.context', 'demisto.context', ([], {}), '()\n', (113, 115), True, 'import demistomock as demisto\n'), ((330, 657), 'demistomock.executeCommand', 'demisto.executeCommand', (['"""demisto-api-post"""', "{'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'}}})\n", (352, 657), True, 'import demistomock as demisto\n'), ((2010, 2031), 'demistomock.results', 'demisto.results', (['data'], {}), '(data)\n', (2025, 2031), True, 'import demistomock as demisto\n'), ((2284, 2305), 'demistomock.results', 'demisto.results', (['data'], {}), '(data)\n', (2299, 2305), True, 'import demistomock as demisto\n'), ((184, 203), 'demistomock.incidents', 'demisto.incidents', ([], {}), '()\n', (201, 203), True, 'import demistomock as demisto\n'), ((1104, 1148), 'demistomock.executeCommand', 'demisto.executeCommand', (['"""DemistoVersion"""', '{}'], {}), "('DemistoVersion', {})\n", (1126, 1148), True, 'import demistomock as demisto\n')]