import APIClient
import Combine
import ComposableArchitecture
import Foundation
/*
 Simple comment count cache that keeps up to 500 hooks in memory.
 - Handles comment count per hookId.
 */
final actor SimpleCommentCache: Sendable {
    private var commentCount: [String: Int] = [:]
    private let maxCacheSize = 500

    func getCommentCount(for hookId: String) -> Int? {
        return commentCount[hookId]
    }

    func setCommentCount(_ count: Int, for hookId: String) {
        if commentCount.count >= maxCacheSize {
            commentCount.removeAll()
        }
        commentCount[hookId] = count
    }

    func getCommentCounts(_ hookIds: [String]) async -> [String: Int] {
        var result: [String: Int] = [:]
        for hookId in hookIds {
            if let count = getCommentCount(for: hookId) {
                result[hookId] = count
            }
        }
        return result
    }

    func hydrateFromHooks(_ hooks: [Hook]) {
        for hook in hooks {
            setCommentCount(hook.commentCount, for: hook.id)
        }
    }

    func removeHook(_ hookId: String) {
        commentCount.removeValue(forKey: hookId)
    }
}
