# --------------------------------------------------------------------------------------------
# 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(
    "monitor account issue create",
)
class Create(AAZCommand):
    """Create a new issue or updates an existing one

    :example: Create a new issue in an Azure Monitor workspace
        az monitor account issue create --azure-monitor-workspace-name myWorkspace -g myRG -n myIssue --title "My Issue" --severity High --status New
    """

    _aaz_info = {
        "version": "2025-10-03",
        "resources": [
            ["mgmt-plane", "/subscriptions/{}/resourcegroups/{}/providers/microsoft.monitor/accounts/{}/issues/{}", "2025-10-03"],
        ]
    }

    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.azure_monitor_workspace_name = AAZStrArg(
            options=["-w", "--azure-monitor-workspace-name"],
            help="The name of the Azure Monitor Workspace. The name is case insensitive",
            required=True,
            fmt=AAZStrArgFormat(
                pattern="^(?!-)[a-zA-Z0-9-]+[^-]$",
            ),
        )
        _args_schema.issue_name = AAZStrArg(
            options=["-n", "--name", "--issue-name"],
            help="The name of the IssueResource",
            required=True,
            fmt=AAZStrArgFormat(
                pattern="^(?!-)[a-zA-Z0-9-]+[^-]$",
            ),
        )
        _args_schema.resource_group = AAZResourceGroupNameArg(
            required=True,
        )
        _args_schema.related = AAZStrArg(
            options=["--related"],
            help="Related resource or alert that is to be added to the issue (default: empty - the issue will be created without any related resources or alerts)",
        )

        # define Arg Group "Properties"

        _args_schema = cls._args_schema
        _args_schema.background = AAZObjectArg(
            options=["--background"],
            arg_group="Properties",
            help="The issue background information",
        )
        _args_schema.impact_time = AAZDateTimeArg(
            options=["--impact-time"],
            arg_group="Properties",
            help="The issue impact time (in UTC)",
            fmt=AAZDateTimeFormat(
                protocol="iso",
            ),
        )
        _args_schema.notifications = AAZObjectArg(
            options=["--notifications"],
            arg_group="Properties",
            help="The issue notification settings",
        )
        _args_schema.severity = AAZStrArg(
            options=["--severity"],
            arg_group="Properties",
            help="The issue severity",
        )
        _args_schema.status = AAZStrArg(
            options=["--status"],
            arg_group="Properties",
            help="The issue status",
            enum={"Canceled": "Canceled", "Closed": "Closed", "InProgress": "InProgress", "Mitigated": "Mitigated", "New": "New"},
        )
        _args_schema.title = AAZStrArg(
            options=["--title"],
            arg_group="Properties",
            help="The issue title",
        )

        background = cls._args_schema.background
        background.details = AAZListArg(
            options=["details"],
            help="The background details",
        )
        background.text = AAZStrArg(
            options=["text"],
            help="The background text",
        )
        background.type = AAZStrArg(
            options=["type"],
            help="The background type",
        )

        details = cls._args_schema.background.details
        details.Element = AAZObjectArg()

        _element = cls._args_schema.background.details.Element
        _element.name = AAZStrArg(
            options=["name"],
            help="The background details name",
            required=True,
        )
        _element.value = AAZStrArg(
            options=["value"],
            help="The background details value",
            required=True,
        )

        notifications = cls._args_schema.notifications
        notifications.action_group_ids = AAZListArg(
            options=["action-group-ids"],
            help="The action group IDs to notify",
        )
        notifications.exclude_default_action_groups = AAZBoolArg(
            options=["exclude-default-action-groups"],
            help="Whether to exclude default action groups from notifications",
        )
        notifications.update_types = AAZListArg(
            options=["update-types"],
            help="The types of updates that trigger notifications",
        )

        action_group_ids = cls._args_schema.notifications.action_group_ids
        action_group_ids.Element = AAZStrArg()

        update_types = cls._args_schema.notifications.update_types
        update_types.Element = AAZObjectArg()

        _element = cls._args_schema.notifications.update_types.Element
        _element.issue_creation = AAZObjectArg(
            options=["issue-creation"],
            blank={},
        )
        _element.on_change = AAZObjectArg(
            options=["on-change"],
            blank={},
        )
        _element.time_based = AAZObjectArg(
            options=["time-based"],
        )

        time_based = cls._args_schema.notifications.update_types.Element.time_based
        time_based.update_interval = AAZStrArg(
            options=["update-interval"],
            help="The interval between time-based updates",
            required=True,
        )
        return cls._args_schema

    def _execute_operations(self):
        self.pre_operations()
        self.IssueCreate(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 IssueCreate(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.Monitor/accounts/{azureMonitorWorkspaceName}/issues/{issueName}",
                **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(
                    "azureMonitorWorkspaceName", self.ctx.args.azure_monitor_workspace_name,
                    required=True,
                ),
                **self.serialize_url_param(
                    "issueName", self.ctx.args.issue_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(
                    "related", self.ctx.args.related,
                ),
                **self.serialize_query_param(
                    "api-version", "2025-10-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("properties", AAZObjectType)

            properties = _builder.get(".properties")
            if properties is not None:
                properties.set_prop("background", AAZObjectType, ".background")
                properties.set_prop("impactTime", AAZStrType, ".impact_time", typ_kwargs={"flags": {"required": True}})
                properties.set_prop("notifications", AAZObjectType, ".notifications")
                properties.set_prop("severity", AAZStrType, ".severity", typ_kwargs={"flags": {"required": True}})
                properties.set_prop("status", AAZStrType, ".status", typ_kwargs={"flags": {"required": True}})
                properties.set_prop("title", AAZStrType, ".title", typ_kwargs={"flags": {"required": True}})

            background = _builder.get(".properties.background")
            if background is not None:
                background.set_prop("details", AAZListType, ".details")
                background.set_prop("text", AAZStrType, ".text")
                background.set_prop("type", AAZStrType, ".type")

            details = _builder.get(".properties.background.details")
            if details is not None:
                details.set_elements(AAZObjectType, ".")

            _elements = _builder.get(".properties.background.details[]")
            if _elements is not None:
                _elements.set_prop("name", AAZStrType, ".name", typ_kwargs={"flags": {"required": True}})
                _elements.set_prop("value", AAZStrType, ".value", typ_kwargs={"flags": {"required": True}})

            notifications = _builder.get(".properties.notifications")
            if notifications is not None:
                notifications.set_prop("actionGroupIds", AAZListType, ".action_group_ids")
                notifications.set_prop("excludeDefaultActionGroups", AAZBoolType, ".exclude_default_action_groups")
                notifications.set_prop("updateTypes", AAZListType, ".update_types")

            action_group_ids = _builder.get(".properties.notifications.actionGroupIds")
            if action_group_ids is not None:
                action_group_ids.set_elements(AAZStrType, ".")

            update_types = _builder.get(".properties.notifications.updateTypes")
            if update_types is not None:
                update_types.set_elements(AAZObjectType, ".")

            _elements = _builder.get(".properties.notifications.updateTypes[]")
            if _elements is not None:
                _elements.set_const("updateType", "IssueCreation", AAZStrType, ".issue_creation", typ_kwargs={"flags": {"required": True}})
                _elements.set_const("updateType", "OnChange", AAZStrType, ".on_change", typ_kwargs={"flags": {"required": True}})
                _elements.set_const("updateType", "TimeBased", AAZStrType, ".time_based", typ_kwargs={"flags": {"required": True}})
                _elements.discriminate_by("updateType", "IssueCreation")
                _elements.discriminate_by("updateType", "OnChange")
                _elements.discriminate_by("updateType", "TimeBased")

            disc_time_based = _builder.get(".properties.notifications.updateTypes[]{updateType:TimeBased}")
            if disc_time_based is not None:
                disc_time_based.set_prop("updateInterval", AAZStrType, ".time_based.update_interval", typ_kwargs={"flags": {"required": True}})

            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.id = AAZStrType(
                flags={"read_only": True},
            )
            _schema_on_200_201.name = AAZStrType(
                flags={"read_only": True},
            )
            _schema_on_200_201.properties = AAZObjectType()
            _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.background = AAZObjectType()
            properties.impact_time = AAZStrType(
                serialized_name="impactTime",
                flags={"required": True},
            )
            properties.investigations = AAZListType(
                flags={"read_only": True},
            )
            properties.investigations_count = AAZIntType(
                serialized_name="investigationsCount",
                flags={"read_only": True},
            )
            properties.notifications = AAZObjectType()
            properties.provisioning_state = AAZStrType(
                serialized_name="provisioningState",
                flags={"read_only": True},
            )
            properties.severity = AAZStrType(
                flags={"required": True},
            )
            properties.status = AAZStrType(
                flags={"required": True},
            )
            properties.title = AAZStrType(
                flags={"required": True},
            )

            background = cls._schema_on_200_201.properties.background
            background.details = AAZListType()
            background.text = AAZStrType()
            background.type = AAZStrType()

            details = cls._schema_on_200_201.properties.background.details
            details.Element = AAZObjectType()

            _element = cls._schema_on_200_201.properties.background.details.Element
            _element.name = AAZStrType(
                flags={"required": True},
            )
            _element.value = AAZStrType(
                flags={"required": True},
            )

            investigations = cls._schema_on_200_201.properties.investigations
            investigations.Element = AAZObjectType()

            _element = cls._schema_on_200_201.properties.investigations.Element
            _element.created_at = AAZStrType(
                serialized_name="createdAt",
                flags={"required": True},
            )
            _element.id = AAZStrType(
                flags={"required": True},
            )

            notifications = cls._schema_on_200_201.properties.notifications
            notifications.action_group_ids = AAZListType(
                serialized_name="actionGroupIds",
            )
            notifications.exclude_default_action_groups = AAZBoolType(
                serialized_name="excludeDefaultActionGroups",
            )
            notifications.update_types = AAZListType(
                serialized_name="updateTypes",
            )

            action_group_ids = cls._schema_on_200_201.properties.notifications.action_group_ids
            action_group_ids.Element = AAZStrType()

            update_types = cls._schema_on_200_201.properties.notifications.update_types
            update_types.Element = AAZObjectType()

            _element = cls._schema_on_200_201.properties.notifications.update_types.Element
            _element.update_type = AAZStrType(
                serialized_name="updateType",
                flags={"required": True},
            )

            disc_time_based = cls._schema_on_200_201.properties.notifications.update_types.Element.discriminate_by("update_type", "TimeBased")
            disc_time_based.update_interval = AAZStrType(
                serialized_name="updateInterval",
                flags={"required": True},
            )

            system_data = cls._schema_on_200_201.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",
            )

            return cls._schema_on_200_201


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


__all__ = ["Create"]
