# --------------------------------------------------------------------------------------------
# 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(
    "storage advanced-platform-metric update",
    is_preview=True,
)
class Update(AAZCommand):
    """Update the advanced platform metrics rule for the storage account.

    :example: Update advanced platform metrics rule
        az storage advanced-platform-metric update -g resource_group_name --account-name storage_account_name --enabled --rule-config-filter-type AllContainersFilter
    """

    _aaz_info = {
        "version": "2026-04-01",
        "resources": [
            ["mgmt-plane", "/subscriptions/{}/resourcegroups/{}/providers/microsoft.storage/storageaccounts/{}/advancedplatformmetrics/{}", "2026-04-01"],
        ]
    }

    AZ_SUPPORT_GENERIC_UPDATE = True

    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.account_name = AAZStrArg(
            options=["--account-name"],
            help="The name of the storage account within the specified resource group. Storage account names must be between 3 and 24 characters in length and use numbers and lower-case letters only.",
            required=True,
            id_part="name",
            fmt=AAZStrArgFormat(
                pattern="^[a-z0-9]+$",
                max_length=24,
                min_length=3,
            ),
        )
        _args_schema.advanced_platform_metrics_rule_type = AAZStrArg(
            options=["-n", "--name", "--advanced-platform-metrics-rule-type"],
            help="The type of the advanced platform metrics rule.",
            required=True,
            id_part="child_name_1",
            enum={"ContainerLevelCapacityMetrics": "ContainerLevelCapacityMetrics"},
        )
        _args_schema.resource_group = AAZResourceGroupNameArg(
            required=True,
        )

        # define Arg Group "Properties"

        _args_schema = cls._args_schema
        _args_schema.enabled = AAZBoolArg(
            options=["--enabled"],
            arg_group="Properties",
            help="A boolean flag which enables the advanced platform metrics rule.",
        )

        # define Arg Group "RuleConfig"

        _args_schema = cls._args_schema
        _args_schema.rule_config_filter_type = AAZStrArg(
            options=["--filter-type", "--rule-config-filter-type"],
            arg_group="RuleConfig",
            help="The type of filter applied to the rule. Possible values include: AllContainersFilter, ContainerPrefixFilter, ContainerListFilter.",
            nullable=True,
            enum={"AllContainersFilter": "AllContainersFilter", "ContainerListFilter": "ContainerListFilter", "ContainerPrefixFilter": "ContainerPrefixFilter"},
        )
        _args_schema.rule_config_filter_values = AAZListArg(
            options=["--filter-values", "--rule-config-filter-values"],
            arg_group="RuleConfig",
            help="The values for the filter applied to the rule. If filter type is AllContainersFilter, filter values should be empty. If filter type is ContainerPrefixFilter, filter values should contain a list of container prefixes. If filter type is ContainerListFilter, filter values should contain a list of container names.",
            nullable=True,
        )

        rule_config_filter_values = cls._args_schema.rule_config_filter_values
        rule_config_filter_values.Element = AAZStrArg(
            nullable=True,
        )
        return cls._args_schema

    def _execute_operations(self):
        self.pre_operations()
        self.AdvancedPlatformMetricsGet(ctx=self.ctx)()
        self.pre_instance_update(self.ctx.vars.instance)
        self.InstanceUpdateByJson(ctx=self.ctx)()
        self.InstanceUpdateByGeneric(ctx=self.ctx)()
        self.post_instance_update(self.ctx.vars.instance)
        self.AdvancedPlatformMetricsCreateOrUpdate(ctx=self.ctx)()
        self.post_operations()

    @register_callback
    def pre_operations(self):
        pass

    @register_callback
    def post_operations(self):
        pass

    @register_callback
    def pre_instance_update(self, instance):
        pass

    @register_callback
    def post_instance_update(self, instance):
        pass

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

    class AdvancedPlatformMetricsGet(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]:
                return self.on_200(session)

            return self.on_error(session.http_response)

        @property
        def url(self):
            return self.client.format_url(
                "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{accountName}/advancedPlatformMetrics/{advancedPlatformMetricsRuleType}",
                **self.url_parameters
            )

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

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

        @property
        def url_parameters(self):
            parameters = {
                **self.serialize_url_param(
                    "accountName", self.ctx.args.account_name,
                    required=True,
                ),
                **self.serialize_url_param(
                    "advancedPlatformMetricsRuleType", self.ctx.args.advanced_platform_metrics_rule_type,
                    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", "2026-04-01",
                    required=True,
                ),
            }
            return parameters

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

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

        _schema_on_200 = None

        @classmethod
        def _build_schema_on_200(cls):
            if cls._schema_on_200 is not None:
                return cls._schema_on_200

            cls._schema_on_200 = AAZObjectType()
            _UpdateHelper._build_schema_advanced_platform_metrics_rule_read(cls._schema_on_200)

            return cls._schema_on_200

    class AdvancedPlatformMetricsCreateOrUpdate(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.Storage/storageAccounts/{accountName}/advancedPlatformMetrics/{advancedPlatformMetricsRuleType}",
                **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(
                    "accountName", self.ctx.args.account_name,
                    required=True,
                ),
                **self.serialize_url_param(
                    "advancedPlatformMetricsRuleType", self.ctx.args.advanced_platform_metrics_rule_type,
                    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", "2026-04-01",
                    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,
                value=self.ctx.vars.instance,
            )

            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()
            _UpdateHelper._build_schema_advanced_platform_metrics_rule_read(cls._schema_on_200_201)

            return cls._schema_on_200_201

    class InstanceUpdateByJson(AAZJsonInstanceUpdateOperation):

        def __call__(self, *args, **kwargs):
            self._update_instance(self.ctx.vars.instance)

        def _update_instance(self, instance):
            _instance_value, _builder = self.new_content_builder(
                self.ctx.args,
                value=instance,
                typ=AAZObjectType
            )
            _builder.set_prop("properties", AAZObjectType)

            properties = _builder.get(".properties")
            if properties is not None:
                properties.set_prop("enabled", AAZBoolType, ".enabled", typ_kwargs={"flags": {"required": True}})
                properties.set_prop("ruleConfig", AAZObjectType, ".", typ_kwargs={"flags": {"required": True}})

            rule_config = _builder.get(".properties.ruleConfig")
            if rule_config is not None:
                rule_config.set_prop("filterType", AAZStrType, ".rule_config_filter_type")
                rule_config.set_prop("filterValues", AAZListType, ".rule_config_filter_values")

            filter_values = _builder.get(".properties.ruleConfig.filterValues")
            if filter_values is not None:
                filter_values.set_elements(AAZStrType, ".")

            return _instance_value

    class InstanceUpdateByGeneric(AAZGenericInstanceUpdateOperation):

        def __call__(self, *args, **kwargs):
            self._update_instance_by_generic(
                self.ctx.vars.instance,
                self.ctx.generic_update_args
            )


class _UpdateHelper:
    """Helper class for Update"""

    _schema_advanced_platform_metrics_rule_read = None

    @classmethod
    def _build_schema_advanced_platform_metrics_rule_read(cls, _schema):
        if cls._schema_advanced_platform_metrics_rule_read is not None:
            _schema.id = cls._schema_advanced_platform_metrics_rule_read.id
            _schema.name = cls._schema_advanced_platform_metrics_rule_read.name
            _schema.properties = cls._schema_advanced_platform_metrics_rule_read.properties
            _schema.system_data = cls._schema_advanced_platform_metrics_rule_read.system_data
            _schema.type = cls._schema_advanced_platform_metrics_rule_read.type
            return

        cls._schema_advanced_platform_metrics_rule_read = _schema_advanced_platform_metrics_rule_read = AAZObjectType()

        advanced_platform_metrics_rule_read = _schema_advanced_platform_metrics_rule_read
        advanced_platform_metrics_rule_read.id = AAZStrType(
            flags={"read_only": True},
        )
        advanced_platform_metrics_rule_read.name = AAZStrType(
            flags={"read_only": True},
        )
        advanced_platform_metrics_rule_read.properties = AAZObjectType()
        advanced_platform_metrics_rule_read.system_data = AAZObjectType(
            serialized_name="systemData",
            flags={"read_only": True},
        )
        advanced_platform_metrics_rule_read.type = AAZStrType(
            flags={"read_only": True},
        )

        properties = _schema_advanced_platform_metrics_rule_read.properties
        properties.enabled = AAZBoolType(
            flags={"required": True},
        )
        properties.last_modified_time = AAZStrType(
            serialized_name="lastModifiedTime",
            flags={"read_only": True},
        )
        properties.metrics_emitted = AAZListType(
            serialized_name="metricsEmitted",
            flags={"read_only": True},
        )
        properties.rule_config = AAZObjectType(
            serialized_name="ruleConfig",
            flags={"required": True},
        )
        properties.rule_type = AAZStrType(
            serialized_name="ruleType",
            flags={"read_only": True},
        )

        metrics_emitted = _schema_advanced_platform_metrics_rule_read.properties.metrics_emitted
        metrics_emitted.Element = AAZStrType()

        rule_config = _schema_advanced_platform_metrics_rule_read.properties.rule_config
        rule_config.filter_type = AAZStrType(
            serialized_name="filterType",
        )
        rule_config.filter_values = AAZListType(
            serialized_name="filterValues",
        )

        filter_values = _schema_advanced_platform_metrics_rule_read.properties.rule_config.filter_values
        filter_values.Element = AAZStrType()

        system_data = _schema_advanced_platform_metrics_rule_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",
        )

        _schema.id = cls._schema_advanced_platform_metrics_rule_read.id
        _schema.name = cls._schema_advanced_platform_metrics_rule_read.name
        _schema.properties = cls._schema_advanced_platform_metrics_rule_read.properties
        _schema.system_data = cls._schema_advanced_platform_metrics_rule_read.system_data
        _schema.type = cls._schema_advanced_platform_metrics_rule_read.type


__all__ = ["Update"]
