# --------------------------------------------------------------------------------------------
# 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(
    "security va sql results show",
    is_preview=True,
)
class Show(AAZCommand):
    """Get the scan results of a single rule in a scan record.

    :example: Show results for rule VA1234 from a given scan id on an Azure SQL database.
        az security va sql results show --resource-id /subscriptions/{sub}/resourceGroups/{rg}/providers/Microsoft.Sql/servers/{server}/databases/{db} --scan-id Scan_2026-01-01T00-00-00Z --rule-id VA1234

    :example: Show results for a single rule on an Arc-enabled SQL server.
        az security va sql results show --resource-id /subscriptions/{sub}/resourceGroups/{rg}/providers/Microsoft.HybridCompute/machines/{arc} --database-name MyDb --scan-id Scan_2026-01-01T00-00-00Z --rule-id VA1234
    """

    _aaz_info = {
        "version": "2026-04-01-preview",
        "resources": [
            ["mgmt-plane", "/{resourceid}/providers/microsoft.security/sqlvulnerabilityassessments/default/scans/{}/scanresults/{}", "2026-04-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_id = AAZStrArg(
            options=["--resource-id"],
            help="The fully qualified Azure Resource manager identifier of the resource.",
            required=True,
        )
        _args_schema.scan_id = AAZStrArg(
            options=["--scan-id"],
            help="The scan Id.",
            required=True,
        )
        _args_schema.scan_result_id = AAZStrArg(
            options=["--rule-id", "--scan-result-id"],
            help="The rule Id of the results.",
            required=True,
        )
        _args_schema.database_name = AAZStrArg(
            options=["--database-name"],
            help="The name of the database to assess. Required when the API is called on the parent resource (e.g., server level) rather than on a specific database resource, since the database name is not part of the resource URI. This is the only way to assess system databases (e.g., master), which cannot be referenced directly in the resource URI.",
        )
        return cls._args_schema

    def _execute_operations(self):
        self.pre_operations()
        self.SqlVulnerabilityAssessmentScanResultsGet(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 SqlVulnerabilityAssessmentScanResultsGet(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(
                "/{resourceId}/providers/Microsoft.Security/sqlVulnerabilityAssessments/default/scans/{scanId}/scanResults/{scanResultId}",
                **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(
                    "resourceId", self.ctx.args.resource_id,
                    required=True,
                ),
                **self.serialize_url_param(
                    "scanId", self.ctx.args.scan_id,
                    required=True,
                ),
                **self.serialize_url_param(
                    "scanResultId", self.ctx.args.scan_result_id,
                    required=True,
                ),
            }
            return parameters

        @property
        def query_parameters(self):
            parameters = {
                **self.serialize_query_param(
                    "databaseName", self.ctx.args.database_name,
                ),
                **self.serialize_query_param(
                    "api-version", "2026-04-01-preview",
                    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()

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

            properties = cls._schema_on_200.properties
            properties.baseline_adjusted_result = AAZObjectType(
                serialized_name="baselineAdjustedResult",
            )
            properties.is_trimmed = AAZBoolType(
                serialized_name="isTrimmed",
            )
            properties.query_results = AAZListType(
                serialized_name="queryResults",
            )
            properties.remediation = AAZObjectType()
            properties.rule_id = AAZStrType(
                serialized_name="ruleId",
            )
            properties.rule_metadata = AAZObjectType(
                serialized_name="ruleMetadata",
            )
            properties.status = AAZStrType()

            baseline_adjusted_result = cls._schema_on_200.properties.baseline_adjusted_result
            baseline_adjusted_result.baseline = AAZObjectType()
            baseline_adjusted_result.results_not_in_baseline = AAZListType(
                serialized_name="resultsNotInBaseline",
            )
            baseline_adjusted_result.results_only_in_baseline = AAZListType(
                serialized_name="resultsOnlyInBaseline",
            )
            baseline_adjusted_result.status = AAZStrType()

            baseline = cls._schema_on_200.properties.baseline_adjusted_result.baseline
            baseline.expected_results = AAZListType(
                serialized_name="expectedResults",
            )
            baseline.updated_time = AAZStrType(
                serialized_name="updatedTime",
            )

            expected_results = cls._schema_on_200.properties.baseline_adjusted_result.baseline.expected_results
            expected_results.Element = AAZListType()

            _element = cls._schema_on_200.properties.baseline_adjusted_result.baseline.expected_results.Element
            _element.Element = AAZStrType()

            results_not_in_baseline = cls._schema_on_200.properties.baseline_adjusted_result.results_not_in_baseline
            results_not_in_baseline.Element = AAZListType()

            _element = cls._schema_on_200.properties.baseline_adjusted_result.results_not_in_baseline.Element
            _element.Element = AAZStrType()

            results_only_in_baseline = cls._schema_on_200.properties.baseline_adjusted_result.results_only_in_baseline
            results_only_in_baseline.Element = AAZListType()

            _element = cls._schema_on_200.properties.baseline_adjusted_result.results_only_in_baseline.Element
            _element.Element = AAZStrType()

            query_results = cls._schema_on_200.properties.query_results
            query_results.Element = AAZListType()

            _element = cls._schema_on_200.properties.query_results.Element
            _element.Element = AAZStrType()

            remediation = cls._schema_on_200.properties.remediation
            remediation.automated = AAZBoolType()
            remediation.description = AAZStrType()
            remediation.portal_link = AAZStrType(
                serialized_name="portalLink",
            )
            remediation.scripts = AAZListType()

            scripts = cls._schema_on_200.properties.remediation.scripts
            scripts.Element = AAZStrType()

            rule_metadata = cls._schema_on_200.properties.rule_metadata
            rule_metadata.benchmark_references = AAZListType(
                serialized_name="benchmarkReferences",
            )
            rule_metadata.category = AAZStrType()
            rule_metadata.description = AAZStrType()
            rule_metadata.query_check = AAZObjectType(
                serialized_name="queryCheck",
            )
            rule_metadata.rationale = AAZStrType()
            rule_metadata.rule_id = AAZStrType(
                serialized_name="ruleId",
            )
            rule_metadata.rule_type = AAZStrType(
                serialized_name="ruleType",
            )
            rule_metadata.severity = AAZStrType()
            rule_metadata.title = AAZStrType()

            benchmark_references = cls._schema_on_200.properties.rule_metadata.benchmark_references
            benchmark_references.Element = AAZObjectType()

            _element = cls._schema_on_200.properties.rule_metadata.benchmark_references.Element
            _element.benchmark = AAZStrType()
            _element.reference = AAZStrType()

            query_check = cls._schema_on_200.properties.rule_metadata.query_check
            query_check.column_names = AAZListType(
                serialized_name="columnNames",
            )
            query_check.expected_result = AAZListType(
                serialized_name="expectedResult",
            )
            query_check.query = AAZStrType()

            column_names = cls._schema_on_200.properties.rule_metadata.query_check.column_names
            column_names.Element = AAZStrType()

            expected_result = cls._schema_on_200.properties.rule_metadata.query_check.expected_result
            expected_result.Element = AAZListType()

            _element = cls._schema_on_200.properties.rule_metadata.query_check.expected_result.Element
            _element.Element = AAZStrType()

            system_data = cls._schema_on_200.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


class _ShowHelper:
    """Helper class for Show"""


__all__ = ["Show"]
