import { type LoaderFunctionArgs, redirect } from "react-router";
import * as AdminRepository from "~/features/admin/AdminRepository.server";
import {
	IMPERSONATED_SESSION_KEY,
	SESSION_KEY,
} from "~/features/auth/core/authenticator.server";
import { authSessionStorage } from "~/features/auth/core/session.server";
import type { Nullish } from "~/utils/types";
import { refreshBannedCache, userIsBanned } from "../core/banned.server";

export const loader = async ({ request }: LoaderFunctionArgs) => {
	const userId = await getUserIdEvenIfBanned(request);

	if (!userId || !userIsBanned(userId)) return redirect("/");

	const bannedStatus = (await AdminRepository.allBannedUsers()).get(userId);

	if (!bannedStatus) {
		await refreshBannedCache();
		return redirect("/");
	}

	return {
		banned: bannedStatus.banned,
		reason: bannedStatus.bannedReason,
	};
};

async function getUserIdEvenIfBanned(
	request: Request,
): Promise<Nullish<number>> {
	const session = await authSessionStorage.getSession(
		request.headers.get("Cookie"),
	);
	return session.get(IMPERSONATED_SESSION_KEY) ?? session.get(SESSION_KEY);
}
