/* @vitest-environment node */

import { describe, it, expect } from "vitest";
import {
	SEARCH_SCOPE,
	DEFAULT_SEARCH_LIMIT,
} from "@/lib/frontend/search/urlState";
import { normalizeSearchUrlStateForUser } from "./normalizeState.js";

describe("lib/frontend/search/normalizeState", () => {
	it("forces SINGLE for branch users on the route branch", () => {
		const normalized = normalizeSearchUrlStateForUser(
			{
				q: "x",
				scope: SEARCH_SCOPE.ALL,
				branch: "NL99",
				branches: ["NL01", "NL02"],
				limit: 200,
				from: null,
				to: null,
			},
			{ routeBranch: "NL01", user: { role: "branch", branchId: "NL01" } },
		);

		expect(normalized).toEqual({
			q: "x",
			scope: SEARCH_SCOPE.SINGLE,
			branch: "NL01",
			branches: [],
			limit: 200,
			from: null,
			to: null,
		});
	});

	it("admin-like: SINGLE uses route branch even if URL carries a different branch (admin)", () => {
		const normalized = normalizeSearchUrlStateForUser(
			{
				q: "x",
				scope: SEARCH_SCOPE.SINGLE,
				branch: "NL77",
				branches: [],
				limit: DEFAULT_SEARCH_LIMIT,
				from: null,
				to: null,
			},
			{ routeBranch: "NL01", user: { role: "admin", branchId: null } },
		);

		expect(normalized.branch).toBe("NL01");
		expect(normalized.scope).toBe(SEARCH_SCOPE.SINGLE);
		expect(normalized.branches).toEqual([]);
	});

	it("admin-like: SINGLE uses route branch even if URL carries a different branch (superadmin)", () => {
		const normalized = normalizeSearchUrlStateForUser(
			{
				q: "x",
				scope: SEARCH_SCOPE.SINGLE,
				branch: "NL77",
				branches: [],
				limit: DEFAULT_SEARCH_LIMIT,
				from: null,
				to: null,
			},
			{ routeBranch: "NL01", user: { role: "superadmin", branchId: null } },
		);

		expect(normalized.branch).toBe("NL01");
		expect(normalized.scope).toBe(SEARCH_SCOPE.SINGLE);
		expect(normalized.branches).toEqual([]);
	});

	it("admin-like: MULTI keeps branches and clears branch (dev)", () => {
		const normalized = normalizeSearchUrlStateForUser(
			{
				q: "x",
				scope: SEARCH_SCOPE.MULTI,
				branch: "NL01",
				branches: ["NL06", "NL20"],
				limit: 50,
				from: null,
				to: null,
			},
			{ routeBranch: "NL99", user: { role: "dev", branchId: null } },
		);

		expect(normalized.scope).toBe(SEARCH_SCOPE.MULTI);
		expect(normalized.branch).toBe(null);
		expect(normalized.branches).toEqual(["NL06", "NL20"]);
		expect(normalized.limit).toBe(50);
	});

	it("admin-like: ALL clears branch and branches", () => {
		const normalized = normalizeSearchUrlStateForUser(
			{
				q: "x",
				scope: SEARCH_SCOPE.ALL,
				branch: "NL01",
				branches: ["NL06"],
				limit: 200,
				from: null,
				to: null,
			},
			{ routeBranch: "NL99", user: { role: "admin", branchId: null } },
		);

		expect(normalized).toEqual({
			q: "x",
			scope: SEARCH_SCOPE.ALL,
			branch: null,
			branches: [],
			limit: 200,
			from: null,
			to: null,
		});
	});

	it("unknown roles: fail-safe to SINGLE on route branch", () => {
		const normalized = normalizeSearchUrlStateForUser(
			{
				q: "x",
				scope: SEARCH_SCOPE.ALL,
				branch: "NL01",
				branches: ["NL06"],
				limit: 999, // invalid -> normalized to default
				from: null,
				to: null,
			},
			{ routeBranch: "NL02", user: { role: "user", branchId: "NL02" } },
		);

		expect(normalized.scope).toBe(SEARCH_SCOPE.SINGLE);
		expect(normalized.branch).toBe("NL02");
		expect(normalized.limit).toBe(DEFAULT_SEARCH_LIMIT);
	});
});
