# --------------------------------------------------------------------------------------------
# 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 aaz-dev-tools
# --------------------------------------------------------------------------------------------

# pylint: skip-file
# flake8: noqa

from azure.cli.core.aaz import *


@register_command(
    "sentinel source-control create",
    is_experimental=True,
)
class Create(AAZCommand):
    """Create a source control.
    """

    _aaz_info = {
        "version": "2022-06-01-preview",
        "resources": [
            ["mgmt-plane", "/subscriptions/{}/resourcegroups/{}/providers/microsoft.operationalinsights/workspaces/{}/providers/microsoft.securityinsights/sourcecontrols/{}", "2022-06-01-preview"],
        ]
    }

    def _handler(self, command_args):
        super()._handler(command_args)
        self._execute_operations()
        return self._output()

    _args_schema = None

    @classmethod
    def _build_arguments_schema(cls, *args, **kwargs):
        if cls._args_schema is not None:
            return cls._args_schema
        cls._args_schema = super()._build_arguments_schema(*args, **kwargs)

        # define Arg Group ""

        _args_schema = cls._args_schema
        _args_schema.resource_group = AAZResourceGroupNameArg(
            required=True,
        )
        _args_schema.source_control_id = AAZStrArg(
            options=["-n", "--name", "--source-control-id"],
            help="Source control Id",
            required=True,
            id_part="child_name_1",
        )
        _args_schema.workspace_name = AAZStrArg(
            options=["-w", "--workspace-name"],
            help="The name of the workspace.",
            required=True,
            is_experimental=True,
            id_part="name",
        )

        # define Arg Group "Properties"

        _args_schema = cls._args_schema
        _args_schema.content_types = AAZListArg(
            options=["--content-types"],
            arg_group="Properties",
            help="Array of source control content types.",
        )
        _args_schema.description = AAZStrArg(
            options=["--description"],
            arg_group="Properties",
            help="A description of the source control",
        )
        _args_schema.display_name = AAZStrArg(
            options=["--display-name"],
            arg_group="Properties",
            help="The display name of the source control",
        )
        _args_schema.id = AAZStrArg(
            options=["--id"],
            arg_group="Properties",
            help="The id (a Guid) of the source control",
        )
        _args_schema.last_deployment_info = AAZObjectArg(
            options=["--last-deployment-info"],
            arg_group="Properties",
            help="Information regarding the latest deployment for the source control.",
        )
        _args_schema.repo_type = AAZStrArg(
            options=["--repo-type"],
            arg_group="Properties",
            help="The repository type of the source control",
            enum={"DevOps": "DevOps", "Github": "Github"},
        )
        _args_schema.repository = AAZObjectArg(
            options=["--repository"],
            arg_group="Properties",
            help="Repository metadata.",
        )
        _args_schema.repository_info = AAZObjectArg(
            options=["--repository-info"],
            arg_group="Properties",
            help="Information regarding the resources created in user's repository.",
        )
        _args_schema.version = AAZStrArg(
            options=["--version"],
            arg_group="Properties",
            help="The version number associated with the source control",
            enum={"V1": "V1", "V2": "V2"},
        )

        content_types = cls._args_schema.content_types
        content_types.Element = AAZStrArg(
            enum={"AnalyticRule": "AnalyticRule", "Workbook": "Workbook"},
        )

        last_deployment_info = cls._args_schema.last_deployment_info
        last_deployment_info.deployment = AAZObjectArg(
            options=["deployment"],
            help="Deployment information.",
        )
        last_deployment_info.deployment_fetch_status = AAZStrArg(
            options=["deployment-fetch-status"],
            help="Status while fetching the last deployment.",
            enum={"NotFound": "NotFound", "Success": "Success", "Unauthorized": "Unauthorized"},
        )
        last_deployment_info.message = AAZStrArg(
            options=["message"],
            help="Additional details about the deployment that can be shown to the user.",
        )

        deployment = cls._args_schema.last_deployment_info.deployment
        deployment.deployment_id = AAZStrArg(
            options=["deployment-id"],
            help="Deployment identifier.",
        )
        deployment.deployment_logs_url = AAZStrArg(
            options=["deployment-logs-url"],
            help="Url to access repository action logs.",
        )
        deployment.deployment_result = AAZStrArg(
            options=["deployment-result"],
            help="The outcome of the deployment.",
            enum={"Canceled": "Canceled", "Failed": "Failed", "Success": "Success"},
        )
        deployment.deployment_state = AAZStrArg(
            options=["deployment-state"],
            help="Current status of the deployment.",
            enum={"Canceling": "Canceling", "Completed": "Completed", "In_Progress": "In_Progress", "Queued": "Queued"},
        )
        deployment.deployment_time = AAZStrArg(
            options=["deployment-time"],
            help="The time when the deployment finished.",
        )

        repository = cls._args_schema.repository
        repository.branch = AAZStrArg(
            options=["branch"],
            help="Branch name of repository.",
        )
        repository.deployment_logs_url = AAZStrArg(
            options=["deployment-logs-url"],
            help="Url to access repository action logs.",
        )
        repository.display_url = AAZStrArg(
            options=["display-url"],
            help="Display url of repository.",
        )
        repository.path_mapping = AAZListArg(
            options=["path-mapping"],
            help="Dictionary of source control content type and path mapping.",
        )
        repository.url = AAZStrArg(
            options=["url"],
            help="Url of repository.",
        )

        path_mapping = cls._args_schema.repository.path_mapping
        path_mapping.Element = AAZObjectArg()

        _element = cls._args_schema.repository.path_mapping.Element
        _element.content_type = AAZStrArg(
            options=["content-type"],
            help="Content type.",
            enum={"AnalyticRule": "AnalyticRule", "Workbook": "Workbook"},
        )
        _element.path = AAZStrArg(
            options=["path"],
            help="The path to the content.",
        )

        repository_info = cls._args_schema.repository_info
        repository_info.azure_dev_ops_resource_info = AAZObjectArg(
            options=["azure-dev-ops-resource-info"],
            help="Resources created in Azure DevOps for this source-control.",
        )
        repository_info.git_hub_resource_info = AAZObjectArg(
            options=["git-hub-resource-info"],
            help="Resources created in GitHub for this source-control.",
        )
        repository_info.webhook = AAZObjectArg(
            options=["webhook"],
            help="The webhook object created for the source-control.",
        )

        azure_dev_ops_resource_info = cls._args_schema.repository_info.azure_dev_ops_resource_info
        azure_dev_ops_resource_info.pipeline_id = AAZStrArg(
            options=["pipeline-id"],
            help="Id of the pipeline created for the source-control.",
        )
        azure_dev_ops_resource_info.service_connection_id = AAZStrArg(
            options=["service-connection-id"],
            help="Id of the service-connection created for the source-control.",
        )

        git_hub_resource_info = cls._args_schema.repository_info.git_hub_resource_info
        git_hub_resource_info.app_installation_id = AAZStrArg(
            options=["app-installation-id"],
            help="GitHub application installation id.",
        )

        webhook = cls._args_schema.repository_info.webhook
        webhook.rotate_webhook_secret = AAZBoolArg(
            options=["rotate-webhook-secret"],
            help="A flag to instruct the backend service to rotate webhook secret.",
        )
        webhook.webhook_id = AAZStrArg(
            options=["webhook-id"],
            help="Unique identifier for the webhook.",
        )
        webhook.webhook_secret_update_time = AAZStrArg(
            options=["webhook-secret-update-time"],
            help="Time when the webhook secret was updated.",
        )
        webhook.webhook_url = AAZStrArg(
            options=["webhook-url"],
            help="URL that gets invoked by the webhook.",
        )

        # define Arg Group "SourceControl"

        _args_schema = cls._args_schema
        _args_schema.etag = AAZStrArg(
            options=["--etag"],
            arg_group="SourceControl",
            help="Etag of the azure resource",
        )
        return cls._args_schema

    def _execute_operations(self):
        self.SourceControlsCreate(ctx=self.ctx)()

    def _output(self, *args, **kwargs):
        result = self.deserialize_output(self.ctx.vars.instance, client_flatten=True)
        return result

    class SourceControlsCreate(AAZHttpOperation):
        CLIENT_TYPE = "MgmtClient"

        def __call__(self, *args, **kwargs):
            request = self.make_request()
            session = self.client.send_request(request=request, stream=False, **kwargs)
            if session.http_response.status_code in [200, 201]:
                return self.on_200_201(session)

            return self.on_error(session.http_response)

        @property
        def url(self):
            return self.client.format_url(
                "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.OperationalInsights/workspaces/{workspaceName}/providers/Microsoft.SecurityInsights/sourcecontrols/{sourceControlId}",
                **self.url_parameters
            )

        @property
        def method(self):
            return "PUT"

        @property
        def error_format(self):
            return "ODataV4Format"

        @property
        def url_parameters(self):
            parameters = {
                **self.serialize_url_param(
                    "resourceGroupName", self.ctx.args.resource_group,
                    required=True,
                ),
                **self.serialize_url_param(
                    "sourceControlId", self.ctx.args.source_control_id,
                    required=True,
                ),
                **self.serialize_url_param(
                    "subscriptionId", self.ctx.subscription_id,
                    required=True,
                ),
                **self.serialize_url_param(
                    "workspaceName", self.ctx.args.workspace_name,
                    required=True,
                ),
            }
            return parameters

        @property
        def query_parameters(self):
            parameters = {
                **self.serialize_query_param(
                    "api-version", "2022-06-01-preview",
                    required=True,
                ),
            }
            return parameters

        @property
        def header_parameters(self):
            parameters = {
                **self.serialize_header_param(
                    "Content-Type", "application/json",
                ),
                **self.serialize_header_param(
                    "Accept", "application/json",
                ),
            }
            return parameters

        @property
        def content(self):
            _content_value, _builder = self.new_content_builder(
                self.ctx.args,
                typ=AAZObjectType,
                typ_kwargs={"flags": {"required": True, "client_flatten": True}}
            )
            _builder.set_prop("etag", AAZStrType, ".etag")
            _builder.set_prop("properties", AAZObjectType, typ_kwargs={"flags": {"client_flatten": True}})

            properties = _builder.get(".properties")
            if properties is not None:
                properties.set_prop("contentTypes", AAZListType, ".content_types", typ_kwargs={"flags": {"required": True}})
                properties.set_prop("description", AAZStrType, ".description")
                properties.set_prop("displayName", AAZStrType, ".display_name", typ_kwargs={"flags": {"required": True}})
                properties.set_prop("id", AAZStrType, ".id")
                properties.set_prop("lastDeploymentInfo", AAZObjectType, ".last_deployment_info")
                properties.set_prop("repoType", AAZStrType, ".repo_type", typ_kwargs={"flags": {"required": True}})
                properties.set_prop("repository", AAZObjectType, ".repository", typ_kwargs={"flags": {"required": True}})
                properties.set_prop("repositoryResourceInfo", AAZObjectType, ".repository_info")
                properties.set_prop("version", AAZStrType, ".version")

            content_types = _builder.get(".properties.contentTypes")
            if content_types is not None:
                content_types.set_elements(AAZStrType, ".")

            last_deployment_info = _builder.get(".properties.lastDeploymentInfo")
            if last_deployment_info is not None:
                last_deployment_info.set_prop("deployment", AAZObjectType, ".deployment")
                last_deployment_info.set_prop("deploymentFetchStatus", AAZStrType, ".deployment_fetch_status")
                last_deployment_info.set_prop("message", AAZStrType, ".message")

            deployment = _builder.get(".properties.lastDeploymentInfo.deployment")
            if deployment is not None:
                deployment.set_prop("deploymentId", AAZStrType, ".deployment_id")
                deployment.set_prop("deploymentLogsUrl", AAZStrType, ".deployment_logs_url")
                deployment.set_prop("deploymentResult", AAZStrType, ".deployment_result")
                deployment.set_prop("deploymentState", AAZStrType, ".deployment_state")
                deployment.set_prop("deploymentTime", AAZStrType, ".deployment_time")

            repository = _builder.get(".properties.repository")
            if repository is not None:
                repository.set_prop("branch", AAZStrType, ".branch")
                repository.set_prop("deploymentLogsUrl", AAZStrType, ".deployment_logs_url")
                repository.set_prop("displayUrl", AAZStrType, ".display_url")
                repository.set_prop("pathMapping", AAZListType, ".path_mapping")
                repository.set_prop("url", AAZStrType, ".url")

            path_mapping = _builder.get(".properties.repository.pathMapping")
            if path_mapping is not None:
                path_mapping.set_elements(AAZObjectType)

            _elements = _builder.get(".properties.repository.pathMapping[]")
            if _elements is not None:
                _elements.set_prop("contentType", AAZStrType, ".content_type")
                _elements.set_prop("path", AAZStrType, ".path")

            repository_resource_info = _builder.get(".properties.repositoryResourceInfo")
            if repository_resource_info is not None:
                repository_resource_info.set_prop("azureDevOpsResourceInfo", AAZObjectType, ".azure_dev_ops_resource_info")
                repository_resource_info.set_prop("gitHubResourceInfo", AAZObjectType, ".git_hub_resource_info")
                repository_resource_info.set_prop("webhook", AAZObjectType, ".webhook")

            azure_dev_ops_resource_info = _builder.get(".properties.repositoryResourceInfo.azureDevOpsResourceInfo")
            if azure_dev_ops_resource_info is not None:
                azure_dev_ops_resource_info.set_prop("pipelineId", AAZStrType, ".pipeline_id")
                azure_dev_ops_resource_info.set_prop("serviceConnectionId", AAZStrType, ".service_connection_id")

            git_hub_resource_info = _builder.get(".properties.repositoryResourceInfo.gitHubResourceInfo")
            if git_hub_resource_info is not None:
                git_hub_resource_info.set_prop("appInstallationId", AAZStrType, ".app_installation_id")

            webhook = _builder.get(".properties.repositoryResourceInfo.webhook")
            if webhook is not None:
                webhook.set_prop("rotateWebhookSecret", AAZBoolType, ".rotate_webhook_secret")
                webhook.set_prop("webhookId", AAZStrType, ".webhook_id")
                webhook.set_prop("webhookSecretUpdateTime", AAZStrType, ".webhook_secret_update_time")
                webhook.set_prop("webhookUrl", AAZStrType, ".webhook_url")

            return self.serialize_content(_content_value)

        def on_200_201(self, session):
            data = self.deserialize_http_content(session)
            self.ctx.set_var(
                "instance",
                data,
                schema_builder=self._build_schema_on_200_201
            )

        _schema_on_200_201 = None

        @classmethod
        def _build_schema_on_200_201(cls):
            if cls._schema_on_200_201 is not None:
                return cls._schema_on_200_201

            cls._schema_on_200_201 = AAZObjectType()

            _schema_on_200_201 = cls._schema_on_200_201
            _schema_on_200_201.etag = AAZStrType()
            _schema_on_200_201.id = AAZStrType(
                flags={"read_only": True},
            )
            _schema_on_200_201.name = AAZStrType(
                flags={"read_only": True},
            )
            _schema_on_200_201.properties = AAZObjectType(
                flags={"client_flatten": True},
            )
            _schema_on_200_201.system_data = AAZObjectType(
                serialized_name="systemData",
                flags={"read_only": True},
            )
            _schema_on_200_201.type = AAZStrType(
                flags={"read_only": True},
            )

            properties = cls._schema_on_200_201.properties
            properties.content_types = AAZListType(
                serialized_name="contentTypes",
                flags={"required": True},
            )
            properties.description = AAZStrType()
            properties.display_name = AAZStrType(
                serialized_name="displayName",
                flags={"required": True},
            )
            properties.id = AAZStrType()
            properties.last_deployment_info = AAZObjectType(
                serialized_name="lastDeploymentInfo",
            )
            properties.repo_type = AAZStrType(
                serialized_name="repoType",
                flags={"required": True},
            )
            properties.repository = AAZObjectType(
                flags={"required": True},
            )
            properties.repository_resource_info = AAZObjectType(
                serialized_name="repositoryResourceInfo",
            )
            properties.version = AAZStrType()

            content_types = cls._schema_on_200_201.properties.content_types
            content_types.Element = AAZStrType()

            last_deployment_info = cls._schema_on_200_201.properties.last_deployment_info
            last_deployment_info.deployment = AAZObjectType()
            last_deployment_info.deployment_fetch_status = AAZStrType(
                serialized_name="deploymentFetchStatus",
            )
            last_deployment_info.message = AAZStrType()

            deployment = cls._schema_on_200_201.properties.last_deployment_info.deployment
            deployment.deployment_id = AAZStrType(
                serialized_name="deploymentId",
            )
            deployment.deployment_logs_url = AAZStrType(
                serialized_name="deploymentLogsUrl",
            )
            deployment.deployment_result = AAZStrType(
                serialized_name="deploymentResult",
            )
            deployment.deployment_state = AAZStrType(
                serialized_name="deploymentState",
            )
            deployment.deployment_time = AAZStrType(
                serialized_name="deploymentTime",
            )

            repository = cls._schema_on_200_201.properties.repository
            repository.branch = AAZStrType()
            repository.deployment_logs_url = AAZStrType(
                serialized_name="deploymentLogsUrl",
            )
            repository.display_url = AAZStrType(
                serialized_name="displayUrl",
            )
            repository.path_mapping = AAZListType(
                serialized_name="pathMapping",
            )
            repository.url = AAZStrType()

            path_mapping = cls._schema_on_200_201.properties.repository.path_mapping
            path_mapping.Element = AAZObjectType()

            _element = cls._schema_on_200_201.properties.repository.path_mapping.Element
            _element.content_type = AAZStrType(
                serialized_name="contentType",
            )
            _element.path = AAZStrType()

            repository_resource_info = cls._schema_on_200_201.properties.repository_resource_info
            repository_resource_info.azure_dev_ops_resource_info = AAZObjectType(
                serialized_name="azureDevOpsResourceInfo",
            )
            repository_resource_info.git_hub_resource_info = AAZObjectType(
                serialized_name="gitHubResourceInfo",
            )
            repository_resource_info.webhook = AAZObjectType()

            azure_dev_ops_resource_info = cls._schema_on_200_201.properties.repository_resource_info.azure_dev_ops_resource_info
            azure_dev_ops_resource_info.pipeline_id = AAZStrType(
                serialized_name="pipelineId",
            )
            azure_dev_ops_resource_info.service_connection_id = AAZStrType(
                serialized_name="serviceConnectionId",
            )

            git_hub_resource_info = cls._schema_on_200_201.properties.repository_resource_info.git_hub_resource_info
            git_hub_resource_info.app_installation_id = AAZStrType(
                serialized_name="appInstallationId",
            )

            webhook = cls._schema_on_200_201.properties.repository_resource_info.webhook
            webhook.rotate_webhook_secret = AAZBoolType(
                serialized_name="rotateWebhookSecret",
            )
            webhook.webhook_id = AAZStrType(
                serialized_name="webhookId",
            )
            webhook.webhook_secret_update_time = AAZStrType(
                serialized_name="webhookSecretUpdateTime",
            )
            webhook.webhook_url = AAZStrType(
                serialized_name="webhookUrl",
            )

            system_data = cls._schema_on_200_201.system_data
            system_data.created_at = AAZStrType(
                serialized_name="createdAt",
                flags={"read_only": True},
            )
            system_data.created_by = AAZStrType(
                serialized_name="createdBy",
                flags={"read_only": True},
            )
            system_data.created_by_type = AAZStrType(
                serialized_name="createdByType",
                flags={"read_only": True},
            )
            system_data.last_modified_at = AAZStrType(
                serialized_name="lastModifiedAt",
                flags={"read_only": True},
            )
            system_data.last_modified_by = AAZStrType(
                serialized_name="lastModifiedBy",
                flags={"read_only": True},
            )
            system_data.last_modified_by_type = AAZStrType(
                serialized_name="lastModifiedByType",
                flags={"read_only": True},
            )

            return cls._schema_on_200_201


__all__ = ["Create"]
