# --------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
#
# Code generated by Microsoft (R) AutoRest Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is
# regenerated.
# --------------------------------------------------------------------------

import os
import unittest

from azure_devtools.scenario_tests import AllowLargeResponse
from azure.cli.testsdk import ScenarioTest
from .. import try_manual
from azure.cli.testsdk import ResourceGroupPreparer


TEST_DIR = os.path.abspath(os.path.join(os.path.abspath(__file__), '..'))


def setup_scenario(test, rg):
    test.kwargs.update({
        'eventhub_namespace': test.create_random_name(prefix='codegenlivetest', length=20),
        'eventhub_name': test.create_random_name(prefix='livetest', length=15)
    })
    test.cmd('az eventhubs namespace create --name {eventhub_namespace} -g {rg}')
    test.cmd('az eventhubs eventhub create --name {eventhub_name} --namespace-name {eventhub_namespace} -g {rg}')
    step_kustoclusterscreateorupdate2(test,rg)
    try:
        output = test.cmd('az ad app create --display-name {mySpName} ').get_output_in_json()
        test.kwargs.update({'myPrincipalId': output.get('appId')})
        # "odata.metadata": "https://graph.windows.net/54826b22-38d6-4fb2-bad9-b7b93a3e9c5a/$metadata#directoryObjects/@Element",
        test.kwargs.update({'myTenantId': output.get('odata.metadata').split('/')[3]})
    except:
        pass

def cleanup_scenario(test, rg):
    try:
        test.cmd('az eventhubs eventhub delete --name {eventhub_name} --namespace-name {eventhub_namespace} -g {rg}')
        test.cmd('az eventhubs namespace delete --name {eventhub_namespace} -g {rg}')
    except:
        pass

def step_kustoclusterscreateorupdate2(test, rg):
    test.cmd('az kusto cluster create '
             '--cluster-name "{myCluster2}" '
             '--type "SystemAssigned" '
             '--location "southcentralus" '
             '--enable-purge true '
             '--enable-streaming-ingest true '
             '--key-vault-properties key-name="" key-vault-uri="" key-version="" '
             '--sku name="Standard_D11_v2" capacity=2 tier="Standard" '
             '--resource-group "{rg}"',
             checks=[])


class KustoScenarioTest(ScenarioTest):

    @ResourceGroupPreparer(name_prefix='clitestkusto_kustorptest'[:7], key='rg', parameter_name='rg')
    def test_kusto_Scenario(self, rg):
        self.kwargs.update({
            'subscription_id': self.get_subscription_id()
        })

        self.kwargs.update({
            'myCluster2': 'followercluster103',
            'myCluster3': 'followercluster103',
            'myCluster': 'leadercluster103',
            'myAttachedDatabaseConfiguration': 'attachedDatabaseConfigurations2',
            'myAttachedDatabaseConfiguration2': 'attachedDatabaseConfigurations2',
            'myDataConnection': 'DataConnections8',
            'myDataConnection2': 'kustoeventhubconnection1',
            'mySpName': 'clicodegenkustotest',
            'myPrincipalId': '532997a9-e004-4b10-9d20-f5c5c5d42c43',
            'myTenantId': '54826b22-38d6-4fb2-bad9-b7b93a3e9c5a'
        })

        from ....tests.latest import test_kusto_scenario as g
        g.call_scenario(self, rg)