// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/// Update from the  server, generated from the model in response to client messages.
@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, *)
@available(watchOS, unavailable)
public struct LiveServerMessage: Sendable {
  let serverMessage: BidiGenerateContentServerMessage

  /// The type of message sent from the server.
  /// - Important: Potential future additions to the ``Payload`` enum may not
  ///   trigger a semantic versioning major version update. If ensure forward
  ///   compatibility, client code should avoid exhaustive switch statements
  ///   over this enum by adding a default case.
  public enum Payload: Sendable {
    /// Content generated by the model in response to client messages.
    case content(LiveServerContent)

    /// Request for the client to execute the provided functions.
    case toolCall(LiveServerToolCall)

    /// Notification for the client that a previously issued ``LiveServerToolCall`` should be
    /// cancelled.
    case toolCallCancellation(LiveServerToolCallCancellation)

    /// Server will disconnect soon.
    case goingAwayNotice(LiveServerGoingAwayNotice)
  }

  /// The message sent from the server.
  public let payload: Payload

  /// Metadata on the usage of the cached content.
  public var usageMetadata: GenerateContentResponse.UsageMetadata? { serverMessage.usageMetadata }
}

// MARK: - Internal parsing

@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, *)
@available(watchOS, unavailable)
extension LiveServerMessage {
  init?(from serverMessage: BidiGenerateContentServerMessage) {
    guard let payload = LiveServerMessage.Payload(from: serverMessage.messageType) else {
      return nil
    }

    self.serverMessage = serverMessage
    self.payload = payload
  }
}

@available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, *)
@available(watchOS, unavailable)
extension LiveServerMessage.Payload {
  init?(from serverMessage: BidiGenerateContentServerMessage.MessageType) {
    switch serverMessage {
    case .setupComplete:
      // this is handled internally, and should not be surfaced to users
      return nil
    case let .serverContent(msg):
      self = .content(LiveServerContent(msg))
    case let .toolCall(msg):
      self = .toolCall(LiveServerToolCall(msg))
    case let .toolCallCancellation(msg):
      self = .toolCallCancellation(LiveServerToolCallCancellation(msg))
    case let .goAway(msg):
      self = .goingAwayNotice(LiveServerGoingAwayNotice(msg))
    }
  }
}
