import { cachified } from "@epic-web/cachified";
import type { LoaderFunctionArgs } from "react-router";
import * as BuildRepository from "~/features/builds/BuildRepository.server";
import { getServerTFunction } from "~/modules/i18n/i18next.server";
import { weaponIdToType } from "~/modules/in-game-lists/weapon-ids";
import { cache, IN_MILLISECONDS, ttl } from "~/utils/cache.server";
import { notFoundIfNullLike } from "~/utils/remix.server";
import { weaponNameSlugToId } from "~/utils/unslugify.server";
import { popularBuilds } from "../build-stats-utils";

export const loader = async ({ params }: LoaderFunctionArgs) => {
	const t = getServerTFunction(["builds", "weapons"]);
	const slug = params.slug;
	const weaponId = notFoundIfNullLike(weaponNameSlugToId(slug));

	if (weaponIdToType(weaponId) === "ALT_SKIN") {
		throw new Response(null, { status: 404 });
	}

	const weaponName = t(`weapons:MAIN_${weaponId}`);

	const cachedPopularBuilds = await cachified({
		key: `popular-builds-${weaponId}`,
		cache,
		ttl: ttl(IN_MILLISECONDS.ONE_HOUR),
		async getFreshValue() {
			return popularBuilds(
				await BuildRepository.popularAbilitiesByWeaponId(weaponId),
			);
		},
	});

	return {
		popularBuilds: cachedPopularBuilds,
		weaponName,
		meta: {
			weaponId,
			slug: slug!,
			breadcrumbText: t("builds:linkButton.popularBuilds"),
		},
	};
};
