# --------------------------------------------------------------------------------------------
# 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(
    "blueprint publish",
    is_preview=True,
)
class Publish(AAZCommand):
    """Publish a new version of the blueprint definition with the latest artifacts. Published blueprint definitions are immutable.

    :example: Publish a management group blueprint
        az blueprint publish --management-group MyManagementGroup --blueprint-name MyBlueprint --version v2

    :example: Publish a subscription blueprint
        az blueprint publish --subscription MySubscription --blueprint-name MyBlueprint --version v2
    """

    _aaz_info = {
        "version": "2018-11-01-preview",
        "resources": [
            ["mgmt-plane", "/{resourcescope}/providers/microsoft.blueprint/blueprints/{}/versions/{}", "2018-11-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.blueprint_name = AAZStrArg(
            options=["--blueprint-name"],
            help="Name of the blueprint definition.",
            required=True,
        )
        _args_schema.resource_scope = AAZStrArg(
            options=["--resource-scope"],
            help="The scope of the resource. Valid scopes are: management group (format: '/providers/Microsoft.Management/managementGroups/{managementGroup}'), subscription (format: '/subscriptions/{subscriptionId}').",
            required=True,
        )
        _args_schema.version = AAZStrArg(
            options=["--version"],
            help="Version of the published blueprint definition.",
            required=True,
        )
        _args_schema.change_notes = AAZStrArg(
            options=["--change-notes"],
            help="Version-specific change notes.",
            fmt=AAZStrArgFormat(
                max_length=500,
            ),
        )

        # define Arg Group "Properties"
        return cls._args_schema

    _args_parameter_definition_metadata_create = None

    @classmethod
    def _build_args_parameter_definition_metadata_create(cls, _schema):
        if cls._args_parameter_definition_metadata_create is not None:
            _schema.description = cls._args_parameter_definition_metadata_create.description
            _schema.display_name = cls._args_parameter_definition_metadata_create.display_name
            _schema.strong_type = cls._args_parameter_definition_metadata_create.strong_type
            return

        cls._args_parameter_definition_metadata_create = AAZObjectArg()

        parameter_definition_metadata_create = cls._args_parameter_definition_metadata_create
        parameter_definition_metadata_create.description = AAZStrArg(
            options=["description"],
            help="Description of this parameter/resourceGroup.",
            fmt=AAZStrArgFormat(
                max_length=500,
            ),
        )
        parameter_definition_metadata_create.display_name = AAZStrArg(
            options=["display-name"],
            help="DisplayName of this parameter/resourceGroup.",
            fmt=AAZStrArgFormat(
                max_length=256,
            ),
        )
        parameter_definition_metadata_create.strong_type = AAZStrArg(
            options=["strong-type"],
            help="StrongType for UI to render rich experience during blueprint assignment. Supported strong types are resourceType, principalId and location.",
            fmt=AAZStrArgFormat(
                max_length=64,
            ),
        )

        _schema.description = cls._args_parameter_definition_metadata_create.description
        _schema.display_name = cls._args_parameter_definition_metadata_create.display_name
        _schema.strong_type = cls._args_parameter_definition_metadata_create.strong_type

    def _execute_operations(self):
        self.pre_operations()
        self.PublishedBlueprintsCreate(ctx=self.ctx)()
        self.post_operations()

    @register_callback
    def pre_operations(self):
        pass

    @register_callback
    def post_operations(self):
        pass

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

    class PublishedBlueprintsCreate(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 [201]:
                return self.on_201(session)

            return self.on_error(session.http_response)

        @property
        def url(self):
            return self.client.format_url(
                "/{resourceScope}/providers/Microsoft.Blueprint/blueprints/{blueprintName}/versions/{versionId}",
                **self.url_parameters
            )

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

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

        @property
        def url_parameters(self):
            parameters = {
                **self.serialize_url_param(
                    "blueprintName", self.ctx.args.blueprint_name,
                    required=True,
                ),
                **self.serialize_url_param(
                    "resourceScope", self.ctx.args.resource_scope,
                    skip_quote=True,
                    required=True,
                ),
                **self.serialize_url_param(
                    "versionId", self.ctx.args.version,
                    required=True,
                ),
            }
            return parameters

        @property
        def query_parameters(self):
            parameters = {
                **self.serialize_query_param(
                    "api-version", "2018-11-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": {"client_flatten": True}}
            )
            _builder.set_prop("properties", AAZObjectType, ".", typ_kwargs={"flags": {"required": True, "client_flatten": True}})

            properties = _builder.get(".properties")
            if properties is not None:
                properties.set_prop("blueprintName", AAZStrType, ".blueprint_name")
                properties.set_prop("changeNotes", AAZStrType, ".change_notes")

            return self.serialize_content(_content_value)

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

        _schema_on_201 = None

        @classmethod
        def _build_schema_on_201(cls):
            if cls._schema_on_201 is not None:
                return cls._schema_on_201

            cls._schema_on_201 = AAZObjectType()

            _schema_on_201 = cls._schema_on_201
            _schema_on_201.id = AAZStrType(
                flags={"read_only": True},
            )
            _schema_on_201.name = AAZStrType(
                flags={"read_only": True},
            )
            _schema_on_201.properties = AAZObjectType(
                flags={"required": True, "client_flatten": True},
            )
            _schema_on_201.type = AAZStrType(
                flags={"read_only": True},
            )

            properties = cls._schema_on_201.properties
            properties.blueprint_name = AAZStrType(
                serialized_name="blueprintName",
            )
            properties.change_notes = AAZStrType(
                serialized_name="changeNotes",
            )
            properties.description = AAZStrType()
            properties.display_name = AAZStrType(
                serialized_name="displayName",
            )
            properties.parameters = AAZDictType()
            properties.resource_groups = AAZDictType(
                serialized_name="resourceGroups",
            )
            properties.status = AAZObjectType()
            properties.target_scope = AAZStrType(
                serialized_name="targetScope",
            )

            parameters = cls._schema_on_201.properties.parameters
            parameters.Element = AAZObjectType()

            _element = cls._schema_on_201.properties.parameters.Element
            _element.allowed_values = AAZListType(
                serialized_name="allowedValues",
            )
            _element.default_value = AAZStrType(
                serialized_name="defaultValue",
            )
            _element.metadata = AAZObjectType(
                flags={"client_flatten": True},
            )
            _PublishHelper._build_schema_parameter_definition_metadata_read(_element.metadata)
            _element.type = AAZStrType(
                flags={"required": True},
            )

            allowed_values = cls._schema_on_201.properties.parameters.Element.allowed_values
            allowed_values.Element = AAZStrType()

            resource_groups = cls._schema_on_201.properties.resource_groups
            resource_groups.Element = AAZObjectType()

            _element = cls._schema_on_201.properties.resource_groups.Element
            _element.depends_on = AAZListType(
                serialized_name="dependsOn",
            )
            _element.location = AAZStrType()
            _element.metadata = AAZObjectType(
                flags={"client_flatten": True},
            )
            _PublishHelper._build_schema_parameter_definition_metadata_read(_element.metadata)
            _element.name = AAZStrType()
            _element.tags = AAZDictType()

            depends_on = cls._schema_on_201.properties.resource_groups.Element.depends_on
            depends_on.Element = AAZStrType()

            tags = cls._schema_on_201.properties.resource_groups.Element.tags
            tags.Element = AAZStrType()

            status = cls._schema_on_201.properties.status
            status.last_modified = AAZStrType(
                serialized_name="lastModified",
                flags={"read_only": True},
            )
            status.time_created = AAZStrType(
                serialized_name="timeCreated",
                flags={"read_only": True},
            )

            return cls._schema_on_201


class _PublishHelper:
    """Helper class for Publish"""

    @classmethod
    def _build_schema_parameter_definition_metadata_create(cls, _builder):
        if _builder is None:
            return
        _builder.set_prop("description", AAZStrType, ".description")
        _builder.set_prop("displayName", AAZStrType, ".display_name")
        _builder.set_prop("strongType", AAZStrType, ".strong_type")

    _schema_parameter_definition_metadata_read = None

    @classmethod
    def _build_schema_parameter_definition_metadata_read(cls, _schema):
        if cls._schema_parameter_definition_metadata_read is not None:
            _schema.description = cls._schema_parameter_definition_metadata_read.description
            _schema.display_name = cls._schema_parameter_definition_metadata_read.display_name
            _schema.strong_type = cls._schema_parameter_definition_metadata_read.strong_type
            return

        cls._schema_parameter_definition_metadata_read = _schema_parameter_definition_metadata_read = AAZObjectType()

        parameter_definition_metadata_read = _schema_parameter_definition_metadata_read
        parameter_definition_metadata_read.description = AAZStrType()
        parameter_definition_metadata_read.display_name = AAZStrType(
            serialized_name="displayName",
        )
        parameter_definition_metadata_read.strong_type = AAZStrType(
            serialized_name="strongType",
        )

        _schema.description = cls._schema_parameter_definition_metadata_read.description
        _schema.display_name = cls._schema_parameter_definition_metadata_read.display_name
        _schema.strong_type = cls._schema_parameter_definition_metadata_read.strong_type


__all__ = ["Publish"]
