# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------

import os
import time

from azure.cli.testsdk.scenario_tests import AllowLargeResponse
from azure.cli.testsdk import (ScenarioTest, ResourceGroupPreparer, JMESPathCheck, live_only, StorageAccountPreparer)

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

from azext_containerapp.tests.latest.common import TEST_LOCATION
from .utils import create_containerapp_env

class ContainerAppJobsCRUDOperationsTest(ScenarioTest):
    @AllowLargeResponse(8192)
    @ResourceGroupPreparer(location="northcentralus")
    # test for CRUD operations on Container App Job resource with trigger type as manual
    def test_containerapp_manualjob_withsecret_crudoperations_e2e(self, resource_group):
        import requests

        self.cmd('configure --defaults location={}'.format(TEST_LOCATION))

        env = self.create_random_name(prefix='env', length=24)
        job = self.create_random_name(prefix='job1', length=24)

        create_containerapp_env(self, env, resource_group)

        # create a container app environment for a Container App Job resource
        self.cmd('containerapp env show -n {} -g {}'.format(env, resource_group), checks=[
            JMESPathCheck('name', env)            
        ])

        ## test for CRUD operations on Container App Job resource with trigger type as manual
        # create a Container App Job resource with trigger type as manual
        self.cmd("az containerapp job create --resource-group {} --name {} --environment {} --secrets 'testsecret=testsecretvalue' --replica-timeout 200 --replica-retry-limit 2 --trigger-type manual --parallelism 1 --replica-completion-count 1 --image mcr.microsoft.com/k8se/quickstart-jobs:latest --cpu '0.25' --memory '0.5Gi'".format(resource_group, job, env))

        # verify the container app job resource contains the secret
        self.cmd("az containerapp job show --resource-group {} --name {}".format(resource_group, job), checks=[
            JMESPathCheck('name', job),
            JMESPathCheck('properties.configuration.replicaTimeout', 200),
            JMESPathCheck('properties.configuration.replicaRetryLimit', 2),
            JMESPathCheck('properties.configuration.triggerType', "manual", case_sensitive=False),
            JMESPathCheck('properties.configuration.secrets[0].name', "testsecret"),
        ])

        # get list of Container App Jobs secrets
        job_secret_list = self.cmd("az containerapp job secret list --resource-group {} --name {}".format(resource_group, job), checks=[
            JMESPathCheck('[0].name', "testsecret"),
            ]).get_output_in_json()
        self.assertTrue(len(job_secret_list) == 1)

        # get list of Container App Jobs secrets with secret value
        job_secret_list = self.cmd("az containerapp job secret list --resource-group {} --name {} --show-values".format(resource_group, job), checks=[
            JMESPathCheck('[0].name', "testsecret"),
            JMESPathCheck('[0].value', "testsecretvalue"),
            ]).get_output_in_json()
        self.assertTrue(len(job_secret_list) == 1)

        # Show value of specified secret in a Container App Job
        self.cmd("az containerapp job secret show --resource-group {} --name {} --secret-name testsecret".format(resource_group, job), checks=[
            JMESPathCheck('name', "testsecret"),
            JMESPathCheck('value', "testsecretvalue"),
            ])

        # Update value of existing secret in a Container App Job
        self.cmd("az containerapp job secret set --resource-group {} --name {} --secret 'testsecret=testsecretvaluev2'".format(resource_group, job))

        # check for updated value of existing secret in a Container App Job
        self.cmd("az containerapp job secret show --resource-group {} --name {} --secret-name testsecret".format(resource_group, job), checks=[
            JMESPathCheck('name', "testsecret"),
            JMESPathCheck('value', "testsecretvaluev2"),
        ])

        # add secret for a Container App Job
        job_secret_list_updated = self.cmd("az containerapp job secret set --resource-group {} --name {} --secret 'testsecret2=testsecretvalue2'".format(resource_group, job)).get_output_in_json()
        self.assertTrue(len(job_secret_list_updated) == 2)

        # check for added secret in a Container App Job
        self.cmd("az containerapp job secret show --resource-group {} --name {} --secret-name testsecret2".format(resource_group, job), checks=[
            JMESPathCheck('name', "testsecret2"),
            JMESPathCheck('value', "testsecretvalue2"),
        ])

        # delete secret from a Container App Job
        self.cmd("az containerapp job secret remove --resource-group {} --name {} --secret-name testsecret2 --yes".format(resource_group, job))

        # confirm secret is deleted in a Container App Job, leaving only one secret
        job_secret_list = self.cmd("az containerapp job secret list --resource-group {} --name {} --show-values".format(resource_group, job), checks=[
            JMESPathCheck('[0].name', "testsecret"),
            JMESPathCheck('[0].value', "testsecretvaluev2"),
            ]).get_output_in_json()
        self.assertTrue(len(job_secret_list) == 1)