package topics

import (
	"encoding/json"
	"fmt"
)

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See NOTICE.txt in the project root for license information.

type TopicProperties struct {
	DataResidencyBoundary      *DataResidencyBoundary       `json:"dataResidencyBoundary,omitempty"`
	DisableLocalAuth           *bool                        `json:"disableLocalAuth,omitempty"`
	Endpoint                   *string                      `json:"endpoint,omitempty"`
	EventTypeInfo              *EventTypeInfo               `json:"eventTypeInfo,omitempty"`
	InboundIPRules             *[]InboundIPRule             `json:"inboundIpRules,omitempty"`
	InputSchema                *InputSchema                 `json:"inputSchema,omitempty"`
	InputSchemaMapping         InputSchemaMapping           `json:"inputSchemaMapping"`
	MetricResourceId           *string                      `json:"metricResourceId,omitempty"`
	MinimumTlsVersionAllowed   *TlsVersion                  `json:"minimumTlsVersionAllowed,omitempty"`
	PrivateEndpointConnections *[]PrivateEndpointConnection `json:"privateEndpointConnections,omitempty"`
	ProvisioningState          *TopicProvisioningState      `json:"provisioningState,omitempty"`
	PublicNetworkAccess        *PublicNetworkAccess         `json:"publicNetworkAccess,omitempty"`
}

var _ json.Unmarshaler = &TopicProperties{}

func (s *TopicProperties) UnmarshalJSON(bytes []byte) error {
	var decoded struct {
		DataResidencyBoundary      *DataResidencyBoundary       `json:"dataResidencyBoundary,omitempty"`
		DisableLocalAuth           *bool                        `json:"disableLocalAuth,omitempty"`
		Endpoint                   *string                      `json:"endpoint,omitempty"`
		EventTypeInfo              *EventTypeInfo               `json:"eventTypeInfo,omitempty"`
		InboundIPRules             *[]InboundIPRule             `json:"inboundIpRules,omitempty"`
		InputSchema                *InputSchema                 `json:"inputSchema,omitempty"`
		MetricResourceId           *string                      `json:"metricResourceId,omitempty"`
		MinimumTlsVersionAllowed   *TlsVersion                  `json:"minimumTlsVersionAllowed,omitempty"`
		PrivateEndpointConnections *[]PrivateEndpointConnection `json:"privateEndpointConnections,omitempty"`
		ProvisioningState          *TopicProvisioningState      `json:"provisioningState,omitempty"`
		PublicNetworkAccess        *PublicNetworkAccess         `json:"publicNetworkAccess,omitempty"`
	}
	if err := json.Unmarshal(bytes, &decoded); err != nil {
		return fmt.Errorf("unmarshaling: %+v", err)
	}

	s.DataResidencyBoundary = decoded.DataResidencyBoundary
	s.DisableLocalAuth = decoded.DisableLocalAuth
	s.Endpoint = decoded.Endpoint
	s.EventTypeInfo = decoded.EventTypeInfo
	s.InboundIPRules = decoded.InboundIPRules
	s.InputSchema = decoded.InputSchema
	s.MetricResourceId = decoded.MetricResourceId
	s.MinimumTlsVersionAllowed = decoded.MinimumTlsVersionAllowed
	s.PrivateEndpointConnections = decoded.PrivateEndpointConnections
	s.ProvisioningState = decoded.ProvisioningState
	s.PublicNetworkAccess = decoded.PublicNetworkAccess

	var temp map[string]json.RawMessage
	if err := json.Unmarshal(bytes, &temp); err != nil {
		return fmt.Errorf("unmarshaling TopicProperties into map[string]json.RawMessage: %+v", err)
	}

	if v, ok := temp["inputSchemaMapping"]; ok {
		impl, err := UnmarshalInputSchemaMappingImplementation(v)
		if err != nil {
			return fmt.Errorf("unmarshaling field 'InputSchemaMapping' for 'TopicProperties': %+v", err)
		}
		s.InputSchemaMapping = impl
	}

	return nil
}
