import Foundation
import MacSyncShared

/// Bridges the iMessage server-side send-queue (legacy `icloud.send_queue`
/// table with bespoke `phoneNumber` + `body` columns) to the generic
/// `SendQueueClient` poll/apply/ack loop. The server side is intentionally
/// untouched; this adapter is what lets the Mac client consolidate around
/// the same SendQueueClient pattern that ICalSync / IReminderSync /
/// INoteSync use.
struct IMessageSendTransport: SendQueueTransport {
    typealias PendingItem = PendingSendMessage

    private let apiClient: any APIClientProtocol

    init(apiClient: any APIClientProtocol) {
        self.apiClient = apiClient
    }

    func id(of item: PendingSendMessage) -> String { item.id }

    func fetchPending() async throws -> [PendingSendMessage] {
        guard apiClient.isAuthenticated else { return [] }
        return try await apiClient.getPendingSends()
    }

    func reportResult(id: String, status: String, error: String?) async throws {
        try await apiClient.reportSendResult(messageId: id, status: status, error: error)
    }
}
