import clsx from "clsx";
import type { MetaFunction } from "react-router";
import { Link, useLoaderData } from "react-router";
import { LocaleTime } from "~/components/LocaleTime";
import { metaTags, type SerializeFrom } from "~/utils/remix";
import { PLUS_SERVER_DISCORD_URL, userPage } from "~/utils/urls";

import { loader } from "../loaders/plus.voting.results.server";
import styles from "../plus-voting-results.module.css";

export { loader };

export const meta: MetaFunction = (args) => {
	return metaTags({
		title: "Plus Server voting results",
		ogTitle: "Plus Server voting results",
		description:
			"Plus Server (+1, +2 and +3) voting results for the latest season.",
		location: args.location,
	});
};

export default function PlusVotingResultsPage() {
	const data = useLoaderData<typeof loader>();

	return (
		<div className="stack md">
			<h2 className="text-center">
				Voting results for{" "}
				<LocaleTime
					date={
						new Date(
							data.lastCompletedVoting.year,
							data.lastCompletedVoting.month,
						)
					}
					options={{
						month: "numeric",
						year: "numeric",
					}}
					inline
				/>
			</h2>
			{data.ownScores && data.ownScores.length > 0 ? (
				<>
					<ul className={clsx(styles.ownScores, "stack sm")}>
						{data.ownScores.map((result) => (
							<li key={result.tier}>
								You{" "}
								{result.passedVoting ? (
									<span className={styles.success}>passed</span>
								) : (
									<span className={styles.fail}>didn&apos;t pass</span>
								)}{" "}
								the +{result.tier} voting
								{typeof result.score === "number"
									? `, your score was ${result.score}% ${
											result.betterThan
												? `(better than ${result.betterThan}% others)`
												: ""
										}`
									: ""}
							</li>
						))}
					</ul>
					<div className="text-sm text-center text-lighter">
						<a href={PLUS_SERVER_DISCORD_URL} target="_blank" rel="noreferrer">
							Join the Discord server
						</a>{" "}
						to get the correct role. In some cases you might need to rejoin the
						server.
					</div>
				</>
			) : null}
			{!data.ownScores ? (
				<div className="text-center text-sm">
					You weren&apos;t in the voting this month.
				</div>
			) : null}
			{data.results ? <Results results={data.results} /> : null}
		</div>
	);
}

function Results({
	results,
}: {
	results: NonNullable<SerializeFrom<typeof loader>["results"]>;
}) {
	return (
		<div>
			<div className="text-xs text-lighter">S = Suggested user</div>
			<div className="stack lg">
				{results.map((tiersResults) => (
					<div className="stack md" key={tiersResults.tier}>
						<h3 className={styles.tierHeader}>
							<span>+{tiersResults.tier}</span>
						</h3>
						{(["passed", "failed"] as const).map((status) => (
							<div key={status} className={styles.passedInfoContainer}>
								<h4 className={styles.passedHeader}>
									{status === "passed" ? "Passed" : "Didn't pass"} (
									{tiersResults[status].length})
								</h4>
								{tiersResults[status].map((user) => (
									<Link
										to={userPage(user)}
										className={clsx(styles.userStatus, {
											[styles.userStatusFailed]: status === "failed",
										})}
										key={user.id}
									>
										{user.wasSuggested ? (
											<span className={styles.suggestionS}>S</span>
										) : null}
										{user.username}
									</Link>
								))}
							</div>
						))}
					</div>
				))}
			</div>
		</div>
	);
}
