import Foundation

// Favourite / unfavourite a status. Both endpoints return the hydrated
// Status; the watch uses that to refresh the optimistic-flip cell.
//
// Per the phone-side contract (src/hooks/mutations/useLikeMutation.ts),
// a successful favourite toggles emit a tapestry letter ('L' for like,
// 'u' for un-like). The watch makes the same call so cross-device
// activity surfaces in the same weave.

extension WatchAPIClient {
    func favouriteStatus(_ id: String) async throws -> WatchStatus {
        guard let url = Endpoints.favourite(instance, statusId: id) else {
            throw WatchAPIError.invalidURL
        }
        let (status, _) = try await request(url, method: "POST", decode: WatchStatus.self)
        Task.detached { [weak self] in
            try? await self?.emitTapestryLetter("L")
        }
        return status
    }

    func unfavouriteStatus(_ id: String) async throws -> WatchStatus {
        guard let url = Endpoints.unfavourite(instance, statusId: id) else {
            throw WatchAPIError.invalidURL
        }
        let (status, _) = try await request(url, method: "POST", decode: WatchStatus.self)
        Task.detached { [weak self] in
            try? await self?.emitTapestryLetter("u")
        }
        return status
    }
}
