// Ambient "X people have been here today" presence indicator.
//
// This is intentionally derived entirely from data getNearbyMessages()
// already returns (authorId, createdAt, lastEchoAt, echoCount) - no extra
// Firestore queries or schema changes are needed. The result is meant to
// feel like ambient atmosphere, not a precise analytic, so a reasonable
// approximation (explained below) is fine.

const DAY_MS = 24 * 60 * 60 * 1000;

// Counts "unique presences" in the last 24 hours across two activity types:
//
// 1. POSTS - any message whose `createdAt` is within the last 24h counts
//    its `authorId` toward a Set, so one person posting several messages
//    today still only counts as one presence.
//
// 2. ECHOES - the data model has no per-echo log (no list of who echoed or
//    when each individual echo happened), only a running `echoCount` and a
//    single `lastEchoAt` timestamp per message. We can't recover *who*
//    echoed or *how many distinct people* echoed a given message from that.
//
//    APPROXIMATION: a message whose `lastEchoAt` falls within the last 24h
//    (and that wasn't itself posted in the last 24h - see below) is counted
//    as ONE additional presence, representing "someone engaged with this
//    message today". This undercounts cases where multiple different people
//    echoed the same message today (they'd only add 1, not N), but avoids
//    needing a new subcollection/security rules just for an ambient number.
//    Given the feature is explicitly "a feeling, not a precise count", this
//    tradeoff is acceptable.
//
// Double-counting guard: addMessage() sets `lastEchoAt = createdAt` on a
// brand new message, so a message posted today would otherwise satisfy both
// the "posted today" and "echoed today" checks. The `!postedToday` guard
// below ensures a fresh post is only counted once (as a post).
export function getPresenceCount(messages) {
	const now = Date.now();

	const postedTodayAuthorIds = new Set();
	let echoedTodayCount = 0;

	for (const message of messages) {
		const createdAtMs = message.createdAt?.toMillis() ?? 0;
		const lastEchoAtMs = message.lastEchoAt?.toMillis() ?? createdAtMs;

		const postedToday = now - createdAtMs < DAY_MS;
		const echoedToday = now - lastEchoAtMs < DAY_MS;

		if (postedToday) {
			// dedupe posters by authorId - same person posting multiple
			// messages today is still just one "presence"
			postedTodayAuthorIds.add(message.authorId ?? message.id);
		} else if (echoedToday) {
			// see APPROXIMATION above: counts as one presence regardless of
			// how many times this message was echoed or by how many people
			echoedTodayCount += 1;
		}
	}

	return postedTodayAuthorIds.size + echoedTodayCount;
}

// Formats the count into the subtitle copy, handling the singular/zero cases.
export function getPresenceText(messages) {
	const count = getPresenceCount(messages);

	if (count === 0) return 'No one has been here today';
	if (count === 1) return '1 person has been here today';
	return `${count} people have been here today`;
}
