// --------------------------------------------------------------------------
// 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 Microsoft (R) AutoRest Code Generator.
// Changes may cause incorrect behavior and will be lost if the code is
// regenerated.
// --------------------------------------------------------------------------

import AzureCore
import Foundation
// swiftlint:disable superfluous_disable_command
// swiftlint:disable identifier_name
// swiftlint:disable line_length
// swiftlint:disable cyclomatic_complexity

/// The Communication Services error.
public final class ChatError: Codable, Swift.Error {
    // MARK: Properties

    /// The error code.
    public let code: String
    /// The error message.
    public let message: String
    /// The error target.
    public let target: String?
    /// Further details about specific errors that led to this error.
    public let details: [ChatError]?
    /// The inner error if any.
    public let innerError: ChatError?

    // MARK: Initializers

    /// Initialize a `ChatError` structure.
    /// - Parameters:
    ///   - code: The error code.
    ///   - message: The error message.
    ///   - target: The error target.
    ///   - details: Further details about specific errors that led to this error.
    ///   - innerError: The inner error if any.
    public init(
        code: String, message: String, target: String? = nil, details: [ChatError]? = nil, innerError: ChatError? = nil
    ) {
        self.code = code
        self.message = message
        self.target = target
        self.details = details
        self.innerError = innerError
    }

    // MARK: Codable

    enum CodingKeys: String, CodingKey {
        case code = "code"
        case message = "message"
        case target = "target"
        case details = "details"
        case innerError = "innererror"
    }

    /// Initialize a `ChatError` structure from decoder
    public init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.code = try container.decode(String.self, forKey: .code)
        self.message = try container.decode(String.self, forKey: .message)
        self.target = try? container.decode(String.self, forKey: .target)
        self.details = try? container.decode([ChatError].self, forKey: .details)
        self.innerError = try? container.decode(ChatError.self, forKey: .innerError)
    }

    /// Encode a `ChatError` structure
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(code, forKey: .code)
        try container.encode(message, forKey: .message)
        if target != nil { try? container.encode(target, forKey: .target) }
        if details != nil { try? container.encode(details, forKey: .details) }
        if innerError != nil { try? container.encode(innerError, forKey: .innerError) }
    }
}
