# --------------------------------------------------------------------------------------------
# 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(
    "sig create",
)
class Create(AAZCommand):
    """Create a shared image gallery.

    :example: Create a shared image gallery
        az sig create --resource-group MyResourceGroup --gallery-name MyGallery

    :example: Create a shared image gallery with enabled system assigned identity.
        az sig create --resource-group MyResourceGroup --gallery-name MyGallery123 --system-assigned

    :example: Create a shared image gallery with a user assigned identity.
        az sig create --resource-group MyResourceGroup --gallery-name MyGallery123 --user-assigned id1

    :example: Create a shared image gallery with both system and user assigned identity.
        az sig create --resource-group MyResourceGroup --gallery-name MyGallery123 --system-assigned --user-assigned id1
    """

    _aaz_info = {
        "version": "2025-03-03",
        "resources": [
            ["mgmt-plane", "/subscriptions/{}/resourcegroups/{}/providers/microsoft.compute/galleries/{}", "2025-03-03"],
        ]
    }

    AZ_SUPPORT_NO_WAIT = True

    def _handler(self, command_args):
        super()._handler(command_args)
        return self.build_lro_poller(self._execute_operations, 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.gallery_name = AAZStrArg(
            options=["-r", "--gallery-name"],
            help="The name of the Shared Image Gallery.",
            required=True,
            fmt=AAZStrArgFormat(
                pattern="^[^_\\W][\\w._-]{0,79}(?<![-.])$",
            ),
        )
        _args_schema.resource_group = AAZResourceGroupNameArg(
            required=True,
        )

        # define Arg Group "CommunityGalleryInfo"

        _args_schema = cls._args_schema
        _args_schema.eula = AAZStrArg(
            options=["--eula"],
            arg_group="CommunityGalleryInfo",
            help="Community gallery publisher eula",
        )
        _args_schema.public_name_prefix = AAZStrArg(
            options=["--public-name-prefix"],
            arg_group="CommunityGalleryInfo",
            help="Community gallery public name prefix",
        )
        _args_schema.publisher_contact = AAZStrArg(
            options=["--publisher-email", "--publisher-contact"],
            arg_group="CommunityGalleryInfo",
            help="Community gallery publisher contact email",
        )
        _args_schema.publisher_uri = AAZStrArg(
            options=["--publisher-uri"],
            arg_group="CommunityGalleryInfo",
            help="Community gallery publisher uri",
        )

        # define Arg Group "Gallery"

        _args_schema = cls._args_schema
        _args_schema.location = AAZResourceLocationArg(
            arg_group="Gallery",
            help="Resource location",
            required=True,
            fmt=AAZResourceLocationArgFormat(
                resource_group_arg="resource_group",
            ),
        )
        _args_schema.tags = AAZDictArg(
            options=["--tags"],
            arg_group="Gallery",
            help="Space-separated tags: key[=value] [key[=value] ...]. Use \"\" to clear existing tags.",
        )

        tags = cls._args_schema.tags
        tags.Element = AAZStrArg()

        # define Arg Group "Identity"

        _args_schema = cls._args_schema
        _args_schema.mi_system_assigned = AAZStrArg(
            options=["--system-assigned", "--mi-system-assigned"],
            arg_group="Identity",
            help="Set the system managed identity.",
            blank="True",
        )
        _args_schema.mi_user_assigned = AAZListArg(
            options=["--user-assigned", "--mi-user-assigned"],
            arg_group="Identity",
            help="Set the user managed identities.",
            blank=[],
        )

        mi_user_assigned = cls._args_schema.mi_user_assigned
        mi_user_assigned.Element = AAZStrArg()

        # define Arg Group "Properties"

        _args_schema = cls._args_schema
        _args_schema.description = AAZStrArg(
            options=["--description"],
            arg_group="Properties",
            help="The description of the gallery.",
        )

        # define Arg Group "SharingProfile"

        _args_schema = cls._args_schema
        _args_schema.permissions = AAZStrArg(
            options=["--permissions"],
            arg_group="SharingProfile",
            help="This property allows you to specify the permission of sharing gallery.",
            enum={"Community": "Community", "Groups": "Groups", "Private": "Private"},
        )

        # define Arg Group "SoftDeletePolicy"

        _args_schema = cls._args_schema
        _args_schema.soft_delete = AAZBoolArg(
            options=["--soft-delete"],
            arg_group="SoftDeletePolicy",
            help="Enable soft-deletion for resources in this gallery, allowing them to be recovered within retention time.",
        )
        return cls._args_schema

    def _execute_operations(self):
        self.pre_operations()
        yield self.GalleriesCreateOrUpdate(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 GalleriesCreateOrUpdate(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 [202]:
                return self.client.build_lro_polling(
                    self.ctx.args.no_wait,
                    session,
                    self.on_200_201,
                    self.on_error,
                    lro_options={"final-state-via": "location"},
                    path_format_arguments=self.url_parameters,
                )
            if session.http_response.status_code in [200, 201]:
                return self.client.build_lro_polling(
                    self.ctx.args.no_wait,
                    session,
                    self.on_200_201,
                    self.on_error,
                    lro_options={"final-state-via": "location"},
                    path_format_arguments=self.url_parameters,
                )

            return self.on_error(session.http_response)

        @property
        def url(self):
            return self.client.format_url(
                "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Compute/galleries/{galleryName}",
                **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(
                    "galleryName", self.ctx.args.gallery_name,
                    required=True,
                ),
                **self.serialize_url_param(
                    "resourceGroupName", self.ctx.args.resource_group,
                    required=True,
                ),
                **self.serialize_url_param(
                    "subscriptionId", self.ctx.subscription_id,
                    required=True,
                ),
            }
            return parameters

        @property
        def query_parameters(self):
            parameters = {
                **self.serialize_query_param(
                    "api-version", "2025-03-03",
                    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("identity", AAZIdentityObjectType)
            _builder.set_prop("location", AAZStrType, ".location", typ_kwargs={"flags": {"required": True}})
            _builder.set_prop("properties", AAZObjectType, typ_kwargs={"flags": {"client_flatten": True}})
            _builder.set_prop("tags", AAZDictType, ".tags")

            identity = _builder.get(".identity")
            if identity is not None:
                identity.set_prop("userAssigned", AAZListType, ".mi_user_assigned", typ_kwargs={"flags": {"action": "create"}})
                identity.set_prop("systemAssigned", AAZStrType, ".mi_system_assigned", typ_kwargs={"flags": {"action": "create"}})

            user_assigned = _builder.get(".identity.userAssigned")
            if user_assigned is not None:
                user_assigned.set_elements(AAZStrType, ".")

            properties = _builder.get(".properties")
            if properties is not None:
                properties.set_prop("description", AAZStrType, ".description")
                properties.set_prop("sharingProfile", AAZObjectType)
                properties.set_prop("softDeletePolicy", AAZObjectType)

            sharing_profile = _builder.get(".properties.sharingProfile")
            if sharing_profile is not None:
                sharing_profile.set_prop("communityGalleryInfo", AAZObjectType)
                sharing_profile.set_prop("permissions", AAZStrType, ".permissions")

            community_gallery_info = _builder.get(".properties.sharingProfile.communityGalleryInfo")
            if community_gallery_info is not None:
                community_gallery_info.set_prop("eula", AAZStrType, ".eula")
                community_gallery_info.set_prop("publicNamePrefix", AAZStrType, ".public_name_prefix")
                community_gallery_info.set_prop("publisherContact", AAZStrType, ".publisher_contact")
                community_gallery_info.set_prop("publisherUri", AAZStrType, ".publisher_uri")

            soft_delete_policy = _builder.get(".properties.softDeletePolicy")
            if soft_delete_policy is not None:
                soft_delete_policy.set_prop("isSoftDeleteEnabled", AAZBoolType, ".soft_delete")

            tags = _builder.get(".tags")
            if tags is not None:
                tags.set_elements(AAZStrType, ".")

            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()
            _CreateHelper._build_schema_gallery_read(cls._schema_on_200_201)

            return cls._schema_on_200_201


class _CreateHelper:
    """Helper class for Create"""

    _schema_gallery_read = None

    @classmethod
    def _build_schema_gallery_read(cls, _schema):
        if cls._schema_gallery_read is not None:
            _schema.id = cls._schema_gallery_read.id
            _schema.identity = cls._schema_gallery_read.identity
            _schema.location = cls._schema_gallery_read.location
            _schema.name = cls._schema_gallery_read.name
            _schema.properties = cls._schema_gallery_read.properties
            _schema.system_data = cls._schema_gallery_read.system_data
            _schema.tags = cls._schema_gallery_read.tags
            _schema.type = cls._schema_gallery_read.type
            return

        cls._schema_gallery_read = _schema_gallery_read = AAZObjectType()

        gallery_read = _schema_gallery_read
        gallery_read.id = AAZStrType(
            flags={"read_only": True},
        )
        gallery_read.identity = AAZIdentityObjectType()
        gallery_read.location = AAZStrType(
            flags={"required": True},
        )
        gallery_read.name = AAZStrType(
            flags={"read_only": True},
        )
        gallery_read.properties = AAZObjectType(
            flags={"client_flatten": True},
        )
        gallery_read.system_data = AAZObjectType(
            serialized_name="systemData",
            flags={"read_only": True},
        )
        gallery_read.tags = AAZDictType()
        gallery_read.type = AAZStrType(
            flags={"read_only": True},
        )

        identity = _schema_gallery_read.identity
        identity.principal_id = AAZStrType(
            serialized_name="principalId",
            flags={"read_only": True},
        )
        identity.tenant_id = AAZStrType(
            serialized_name="tenantId",
            flags={"read_only": True},
        )
        identity.type = AAZStrType()
        identity.user_assigned_identities = AAZDictType(
            serialized_name="userAssignedIdentities",
        )

        user_assigned_identities = _schema_gallery_read.identity.user_assigned_identities
        user_assigned_identities.Element = AAZObjectType()

        _element = _schema_gallery_read.identity.user_assigned_identities.Element
        _element.client_id = AAZStrType(
            serialized_name="clientId",
            flags={"read_only": True},
        )
        _element.principal_id = AAZStrType(
            serialized_name="principalId",
            flags={"read_only": True},
        )

        properties = _schema_gallery_read.properties
        properties.description = AAZStrType()
        properties.identifier = AAZObjectType()
        properties.provisioning_state = AAZStrType(
            serialized_name="provisioningState",
            flags={"read_only": True},
        )
        properties.sharing_profile = AAZObjectType(
            serialized_name="sharingProfile",
        )
        properties.sharing_status = AAZObjectType(
            serialized_name="sharingStatus",
            flags={"read_only": True},
        )
        properties.soft_delete_policy = AAZObjectType(
            serialized_name="softDeletePolicy",
        )

        identifier = _schema_gallery_read.properties.identifier
        identifier.unique_name = AAZStrType(
            serialized_name="uniqueName",
            flags={"read_only": True},
        )

        sharing_profile = _schema_gallery_read.properties.sharing_profile
        sharing_profile.community_gallery_info = AAZObjectType(
            serialized_name="communityGalleryInfo",
        )
        sharing_profile.groups = AAZListType(
            flags={"read_only": True},
        )
        sharing_profile.permissions = AAZStrType()

        community_gallery_info = _schema_gallery_read.properties.sharing_profile.community_gallery_info
        community_gallery_info.community_gallery_enabled = AAZBoolType(
            serialized_name="communityGalleryEnabled",
            flags={"read_only": True},
        )
        community_gallery_info.eula = AAZStrType()
        community_gallery_info.public_name_prefix = AAZStrType(
            serialized_name="publicNamePrefix",
        )
        community_gallery_info.public_names = AAZListType(
            serialized_name="publicNames",
            flags={"read_only": True},
        )
        community_gallery_info.publisher_contact = AAZStrType(
            serialized_name="publisherContact",
        )
        community_gallery_info.publisher_uri = AAZStrType(
            serialized_name="publisherUri",
        )

        public_names = _schema_gallery_read.properties.sharing_profile.community_gallery_info.public_names
        public_names.Element = AAZStrType()

        groups = _schema_gallery_read.properties.sharing_profile.groups
        groups.Element = AAZObjectType()

        _element = _schema_gallery_read.properties.sharing_profile.groups.Element
        _element.ids = AAZListType()
        _element.type = AAZStrType()

        ids = _schema_gallery_read.properties.sharing_profile.groups.Element.ids
        ids.Element = AAZStrType()

        sharing_status = _schema_gallery_read.properties.sharing_status
        sharing_status.aggregated_state = AAZStrType(
            serialized_name="aggregatedState",
            flags={"read_only": True},
        )
        sharing_status.summary = AAZListType()

        summary = _schema_gallery_read.properties.sharing_status.summary
        summary.Element = AAZObjectType()

        _element = _schema_gallery_read.properties.sharing_status.summary.Element
        _element.details = AAZStrType()
        _element.region = AAZStrType()
        _element.state = AAZStrType(
            flags={"read_only": True},
        )

        soft_delete_policy = _schema_gallery_read.properties.soft_delete_policy
        soft_delete_policy.is_soft_delete_enabled = AAZBoolType(
            serialized_name="isSoftDeleteEnabled",
        )

        system_data = _schema_gallery_read.system_data
        system_data.created_at = AAZStrType(
            serialized_name="createdAt",
        )
        system_data.created_by = AAZStrType(
            serialized_name="createdBy",
        )
        system_data.created_by_type = AAZStrType(
            serialized_name="createdByType",
        )
        system_data.last_modified_at = AAZStrType(
            serialized_name="lastModifiedAt",
        )
        system_data.last_modified_by = AAZStrType(
            serialized_name="lastModifiedBy",
        )
        system_data.last_modified_by_type = AAZStrType(
            serialized_name="lastModifiedByType",
        )

        tags = _schema_gallery_read.tags
        tags.Element = AAZStrType()

        _schema.id = cls._schema_gallery_read.id
        _schema.identity = cls._schema_gallery_read.identity
        _schema.location = cls._schema_gallery_read.location
        _schema.name = cls._schema_gallery_read.name
        _schema.properties = cls._schema_gallery_read.properties
        _schema.system_data = cls._schema_gallery_read.system_data
        _schema.tags = cls._schema_gallery_read.tags
        _schema.type = cls._schema_gallery_read.type


__all__ = ["Create"]
