import assert from "node:assert/strict";
import test from "node:test";
import {
	mergeUsersWithActivity,
	resolveUserSelection,
} from "../src/userState.js";

test("mergeUsersWithActivity attaches session activity to user state", () => {
	const merged = mergeUsersWithActivity(
		[
			{
				jellyfin_id: "jf1",
				username: "alex",
				display_name: "alex",
				email: "alex@example.com",
				email_source: "jellyseerr:user",
				avatar: "",
				avatar_source: "",
				contactable: true,
				source: "jellyfin",
				source_summary: "name=jellyfin",
				name_source: "jellyfin",
				access_source: "",
				jellyseerr_user_id: 7,
				jellyseerr_username: "alex",
				user_type: 3,
				user_type_label: "jellyfin",
				role: "admin",
				permissions: 10,
				permissions_label: "admin, manage_users",
				request_count: 3,
			},
		],
		[
			{
				user: "alex",
				title: "Test Movie",
				type: "Movie",
				state: "playing",
				transcoding: "yes",
				transcoding_type: "audio",
				device: "Chrome",
				session_id: "sess1",
			},
		],
	);

	assert.equal(merged[0].activity_count, 1);
	assert.equal(merged[0].activity_active_count, 1);
	assert.equal(merged[0].activity_label, "Playing");
	assert.equal(merged[0].activity.summary, "1 session · 1 active");
	assert.equal(merged[0].activity.sessions[0].title, "Test Movie");
});

test("mergeUsersWithActivity marks paused sessions as paused", () => {
	const merged = mergeUsersWithActivity(
		[
			{
				jellyfin_id: "jf2",
				username: "sam",
				display_name: "Sam",
				email: "sam@example.com",
				email_source: "jellyseerr:user",
				avatar: "",
				avatar_source: "",
				contactable: true,
				source: "jellyfin",
				source_summary: "name=jellyfin",
				name_source: "jellyfin",
				access_source: "",
				jellyseerr_user_id: 8,
				jellyseerr_username: "sam",
				user_type: 3,
				user_type_label: "jellyfin",
				role: "user",
				permissions: 0,
				permissions_label: "",
				request_count: 0,
			},
		],
		[
			{
				user: "sam",
				title: "Paused Episode",
				type: "Episode",
				state: "paused",
				transcoding: "no",
				transcoding_type: "",
				device: "Firefox",
				session_id: "sess2",
			},
		],
	);

	assert.equal(merged[0].activity_label, "Paused");
	assert.equal(merged[0].activity.summary, "1 session · 1 active");
});

test("resolveUserSelection matches id username and display name", () => {
	const users = mergeUsersWithActivity(
		[
			{
				jellyfin_id: "jf1",
				username: "alex",
				display_name: "Alexander",
				email: "alex@example.com",
				email_source: "jellyseerr:user",
				avatar: "",
				avatar_source: "",
				contactable: true,
				source: "jellyfin",
				source_summary: "name=jellyfin",
				name_source: "jellyfin",
				access_source: "",
				jellyseerr_user_id: 7,
				jellyseerr_username: "alex",
				user_type: 3,
				user_type_label: "jellyfin",
				role: "admin",
				permissions: 10,
				permissions_label: "admin, manage_users",
				request_count: 3,
			},
		],
		[],
	);

	assert.equal(resolveUserSelection(users, "jf1")?.username, "alex");
	assert.equal(resolveUserSelection(users, "alex")?.jellyfin_id, "jf1");
	assert.equal(resolveUserSelection(users, "Alexander")?.jellyfin_id, "jf1");
});
