package standardoperation

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.

var _ ProjectTaskProperties = ValidateMigrationInputSqlServerSqlMISyncTaskProperties{}

type ValidateMigrationInputSqlServerSqlMISyncTaskProperties struct {
	Input  *SqlServerSqlMISyncTaskInput                          `json:"input,omitempty"`
	Output *[]ValidateMigrationInputSqlServerSqlMISyncTaskOutput `json:"output,omitempty"`

	// Fields inherited from ProjectTaskProperties

	Commands *[]CommandProperties `json:"commands,omitempty"`
	Errors   *[]ODataError        `json:"errors,omitempty"`
	State    *TaskState           `json:"state,omitempty"`
	TaskType string               `json:"taskType"`
}

func (s ValidateMigrationInputSqlServerSqlMISyncTaskProperties) ProjectTaskProperties() BaseProjectTaskPropertiesImpl {
	return BaseProjectTaskPropertiesImpl{
		Commands: s.Commands,
		Errors:   s.Errors,
		State:    s.State,
		TaskType: s.TaskType,
	}
}

var _ json.Marshaler = ValidateMigrationInputSqlServerSqlMISyncTaskProperties{}

func (s ValidateMigrationInputSqlServerSqlMISyncTaskProperties) MarshalJSON() ([]byte, error) {
	type wrapper ValidateMigrationInputSqlServerSqlMISyncTaskProperties
	wrapped := wrapper(s)
	encoded, err := json.Marshal(wrapped)
	if err != nil {
		return nil, fmt.Errorf("marshaling ValidateMigrationInputSqlServerSqlMISyncTaskProperties: %+v", err)
	}

	var decoded map[string]interface{}
	if err = json.Unmarshal(encoded, &decoded); err != nil {
		return nil, fmt.Errorf("unmarshaling ValidateMigrationInputSqlServerSqlMISyncTaskProperties: %+v", err)
	}

	decoded["taskType"] = "ValidateMigrationInput.SqlServer.AzureSqlDbMI.Sync.LRS"

	encoded, err = json.Marshal(decoded)
	if err != nil {
		return nil, fmt.Errorf("re-marshaling ValidateMigrationInputSqlServerSqlMISyncTaskProperties: %+v", err)
	}

	return encoded, nil
}

var _ json.Unmarshaler = &ValidateMigrationInputSqlServerSqlMISyncTaskProperties{}

func (s *ValidateMigrationInputSqlServerSqlMISyncTaskProperties) UnmarshalJSON(bytes []byte) error {
	var decoded struct {
		Input    *SqlServerSqlMISyncTaskInput                          `json:"input,omitempty"`
		Output   *[]ValidateMigrationInputSqlServerSqlMISyncTaskOutput `json:"output,omitempty"`
		Errors   *[]ODataError                                         `json:"errors,omitempty"`
		State    *TaskState                                            `json:"state,omitempty"`
		TaskType string                                                `json:"taskType"`
	}
	if err := json.Unmarshal(bytes, &decoded); err != nil {
		return fmt.Errorf("unmarshaling: %+v", err)
	}

	s.Input = decoded.Input
	s.Output = decoded.Output
	s.Errors = decoded.Errors
	s.State = decoded.State
	s.TaskType = decoded.TaskType

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

	if v, ok := temp["commands"]; ok {
		var listTemp []json.RawMessage
		if err := json.Unmarshal(v, &listTemp); err != nil {
			return fmt.Errorf("unmarshaling Commands into list []json.RawMessage: %+v", err)
		}

		output := make([]CommandProperties, 0)
		for i, val := range listTemp {
			impl, err := UnmarshalCommandPropertiesImplementation(val)
			if err != nil {
				return fmt.Errorf("unmarshaling index %d field 'Commands' for 'ValidateMigrationInputSqlServerSqlMISyncTaskProperties': %+v", i, err)
			}
			output = append(output, impl)
		}
		s.Commands = &output
	}

	return nil
}
