import Foundation

/// In-memory `HostStore`. Lives in Sources (not Tests) on purpose — plan
/// T-iOS-7: it doubles for the keychain store in this package's contract
/// tests AND in the App layer's ViewModel tests (which import HostRegistry).
///
/// Actor-isolated so concurrent upserts/removes serialize; the state is a
/// value-type snapshot replaced wholesale on every change (no in-place
/// mutation of shared references).
public actor InMemoryHostStore: HostStore {
    private var hosts: [Host]

    public init(hosts: [Host] = []) {
        self.hosts = hosts
    }

    public func loadAll() async throws -> [Host] {
        hosts
    }

    public func upsert(_ host: Host) async throws -> [Host] {
        let updated = hosts.upserting(host)
        hosts = updated
        return updated
    }

    public func remove(id: UUID) async throws -> [Host] {
        let updated = hosts.removing(id: id)
        hosts = updated
        return updated
    }
}
