/* eslint-disable @next/next/no-img-element */
import Layout from "@/components/High/Layout";
import Tooltip from "@/components/Low/Tooltip";
import {useListSearch} from "@/hooks/useListSearch";
import usePagination from "@/hooks/usePagination";
import {GroupWithUsers, User} from "@/interfaces/user";
import {sessionOptions} from "@/lib/session";
import {USER_TYPE_LABELS} from "@/resources/user";
import {convertToUsers, getGroup} from "@/utils/groups.be";
import {shouldRedirectHome} from "@/utils/navigation.disabled";
import {checkAccess, getTypesOfUser} from "@/utils/permissions";
import {getUserName} from "@/utils/users";
import {getLinkedUsers, getSpecificUsers} from "@/utils/users.be";
import axios from "axios";
import clsx from "clsx";
import {withIronSessionSsr} from "iron-session/next";
import moment from "moment";
import Head from "next/head";
import Link from "next/link";
import {useRouter} from "next/router";
import {Divider} from "primereact/divider";
import {useEffect, useMemo, useState} from "react";
import {BsChevronLeft, BsClockFill, BsEnvelopeFill, BsFillPersonVcardFill, BsPlus, BsStopwatchFill, BsTag, BsTrash, BsX} from "react-icons/bs";
import {toast, ToastContainer} from "react-toastify";

export const getServerSideProps = withIronSessionSsr(async ({req, params}) => {
	const user = req.session.user as User;

	if (!user) {
		return {
			redirect: {
				destination: "/login",
				permanent: false,
			},
		};
	}

	if (shouldRedirectHome(user)) {
		return {
			redirect: {
				destination: "/",
				permanent: false,
			},
		};
	}

	const {id} = params as {id: string};

	const group = await getGroup(id);
	if (!group || (checkAccess(user, getTypesOfUser(["admin", "developer"])) && group.admin !== user.id && !group.participants.includes(user.id))) {
		return {
			redirect: {
				destination: "/groups",
				permanent: false,
			},
		};
	}

	const linkedUsers = await getLinkedUsers(user.id, user.type);
	const users = await getSpecificUsers([...group.participants, group.admin]);
	const groupWithUser = convertToUsers(group, users);

	return {
		props: {user, group: JSON.parse(JSON.stringify(groupWithUser)), users: JSON.parse(JSON.stringify(linkedUsers.users))},
	};
}, sessionOptions);

interface Props {
	user: User;
	group: GroupWithUsers;
	users: User[];
}

export default function Home({user, group, users}: Props) {
	const [isAdding, setIsAdding] = useState(false);
	const [isLoading, setIsLoading] = useState(false);
	const [selectedUsers, setSelectedUsers] = useState<string[]>([]);

	const nonParticipantUsers = useMemo(
		() => users.filter((x) => ![...group.participants.map((g) => g.id), group.admin.id, user.id].includes(x.id)),
		[users, group.participants, group.admin.id, user.id],
	);

	const {rows, renderSearch} = useListSearch<User>(
		[["name"], ["corporateInformation", "companyInformation", "name"]],
		isAdding ? nonParticipantUsers : group.participants,
	);
	const {items, renderMinimal} = usePagination<User>(rows, 20);

	const router = useRouter();

	const allowGroupEdit = useMemo(() => checkAccess(user, ["admin", "developer", "mastercorporate"]) || user.id === group.admin.id, [user, group]);

	const toggleUser = (u: User) => setSelectedUsers((prev) => (prev.includes(u.id) ? prev.filter((p) => p !== u.id) : [...prev, u.id]));

	const removeParticipants = () => {
		if (selectedUsers.length === 0) return;
		if (!allowGroupEdit) return;
		if (!confirm(`Are you sure you want to remove ${selectedUsers.length} participant${selectedUsers.length === 1 ? "" : "s"} from this group?`))
			return;

		setIsLoading(true);

		axios
			.patch(`/api/groups/${group.id}`, {participants: group.participants.map((x) => x.id).filter((x) => !selectedUsers.includes(x))})
			.then(() => {
				toast.success("The group has been updated successfully!");
				router.replace(router.asPath);
			})
			.catch((e) => {
				console.error(e);
				toast.error("Something went wrong!");
			})
			.finally(() => setIsLoading(false));
	};

	const addParticipants = () => {
		if (selectedUsers.length === 0) return;
		if (!allowGroupEdit || !isAdding) return;
		if (!confirm(`Are you sure you want to add ${selectedUsers.length} participant${selectedUsers.length === 1 ? "" : "s"} to this group?`))
			return;

		setIsLoading(true);

		axios
			.patch(`/api/groups/${group.id}`, {participants: [...group.participants.map((x) => x.id), ...selectedUsers]})
			.then(() => {
				toast.success("The group has been updated successfully!");
				router.replace(router.asPath);
			})
			.catch((e) => {
				console.error(e);
				toast.error("Something went wrong!");
			})
			.finally(() => setIsLoading(false));
	};

	const renameGroup = () => {
		if (!allowGroupEdit) return;

		const name = prompt("Rename this group:", group.name);
		if (!name) return;

		setIsLoading(true);
		axios
			.patch(`/api/groups/${group.id}`, {name})
			.then(() => {
				toast.success("The group has been updated successfully!");
				router.replace(router.asPath);
			})
			.catch((e) => {
				console.error(e);
				toast.error("Something went wrong!");
			})
			.finally(() => setIsLoading(false));
	};

	const deleteGroup = () => {
		if (!allowGroupEdit) return;
		if (!confirm("Are you sure you want to delete this group?")) return;

		setIsLoading(true);

		axios
			.delete(`/api/groups/${group.id}`)
			.then(() => {
				toast.success("This group has been successfully deleted!");
				router.replace("/groups");
			})
			.catch((e) => {
				console.error(e);
				toast.error("Something went wrong!");
			})
			.finally(() => setIsLoading(false));
	};

	useEffect(() => setSelectedUsers([]), [isAdding]);

	return (
		<>
			<Head>
				<title>{group.name} | EnCoach</title>
				<meta
					name="description"
					content="A training platform for the IELTS exam provided by the Muscat Training Institute and developed by eCrop."
				/>
				<meta name="viewport" content="width=device-width, initial-scale=1" />
				<link rel="icon" href="/favicon.ico" />
			</Head>
			<ToastContainer />
			{user && (
				<Layout user={user}>
					<section className="flex flex-col gap-0">
						<div className="flex flex-col gap-3">
							<div className="flex items-end justify-between">
								<div className="flex items-center gap-2">
									<Link
										href="/groups"
										className="text-mti-purple hover:text-mti-purple-dark transition ease-in-out duration-300 text-xl">
										<BsChevronLeft />
									</Link>
									<h2 className="font-bold text-2xl">{group.name}</h2>
								</div>
								<span className="flex items-center gap-2">
									<BsFillPersonVcardFill className="text-xl" /> {getUserName(group.admin)}
								</span>
							</div>
							{allowGroupEdit && !isAdding && (
								<div className="flex items-center gap-2">
									<button
										onClick={renameGroup}
										disabled={isLoading}
										className="flex items-center gap-1 px-2 py-2 border rounded-full hover:bg-neutral-100 disabled:hover:bg-transparent disabled:opacity-50 disabled:cursor-not-allowed cursor-pointer transition ease-in-out duration-300">
										<BsTag />
										<span className="text-xs">Rename Group</span>
									</button>
									<button
										onClick={deleteGroup}
										disabled={isLoading}
										className="flex items-center gap-1 px-2 py-2 border border-mti-rose rounded-full bg-mti-rose-light text-white hover:bg-mti-rose-dark disabled:hover:bg-mti-rose-light disabled:opacity-50 disabled:cursor-not-allowed cursor-pointer transition ease-in-out duration-300">
										<BsTrash />
										<span className="text-xs">Delete Group</span>
									</button>
								</div>
							)}
						</div>
						<Divider />
						<div className="flex items-center justify-between mb-4">
							<span className="font-semibold text-xl">Participants</span>
							{allowGroupEdit && !isAdding && (
								<div className="flex items-center gap-2">
									<button
										onClick={() => setIsAdding(true)}
										disabled={isLoading}
										className="flex items-center gap-1 px-2 py-2 border rounded-full hover:bg-neutral-100 disabled:hover:bg-transparent disabled:opacity-50 disabled:cursor-not-allowed cursor-pointer transition ease-in-out duration-300">
										<BsPlus />
										<span className="text-xs">Add Participants</span>
									</button>
									<button
										onClick={removeParticipants}
										disabled={selectedUsers.length === 0 || isLoading}
										className="flex items-center gap-1 px-2 py-2 border border-mti-rose rounded-full bg-mti-rose-light text-white hover:bg-mti-rose-dark disabled:hover:bg-mti-rose-light disabled:opacity-50 disabled:cursor-not-allowed cursor-pointer transition ease-in-out duration-300">
										<BsTrash />
										<span className="text-xs">Remove Participants</span>
									</button>
								</div>
							)}
							{allowGroupEdit && isAdding && (
								<div className="flex items-center gap-2">
									<button
										onClick={() => setIsAdding(false)}
										disabled={isLoading}
										className="flex items-center gap-1 px-2 py-2 border rounded-full border-mti-rose bg-mti-rose-light text-white hover:bg-mti-rose-dark disabled:hover:bg-mti-rose-light disabled:opacity-50 disabled:cursor-not-allowed cursor-pointer transition ease-in-out duration-300">
										<BsX />
										<span className="text-xs">Discard Selection</span>
									</button>
									<button
										onClick={addParticipants}
										disabled={selectedUsers.length === 0 || isLoading}
										className="flex items-center gap-1 px-2 py-2 border rounded-full border-mti-green bg-mti-green-light text-white hover:bg-mti-green-dark disabled:hover:bg-mti-green-light disabled:opacity-50 disabled:cursor-not-allowed cursor-pointer transition ease-in-out duration-300">
										<BsPlus />
										<span className="text-xs">Add Participants</span>
									</button>
								</div>
							)}
						</div>
						<div className="w-full flex items-center gap-4">
							{renderSearch()}
							{renderMinimal()}
						</div>
					</section>

					<section className="w-full h-full grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
						{items.map((u) => (
							<button
								onClick={() => toggleUser(u)}
								disabled={!allowGroupEdit}
								key={u.id}
								className={clsx(
									"p-4 pr-6 h-48 relative border rounded-xl flex flex-col gap-3 justify-between text-left cursor-pointer",
									"hover:border-mti-purple transition ease-in-out duration-300",
									selectedUsers.includes(u.id) && "border-mti-purple",
								)}>
								<div className="flex items-center gap-2">
									<div className="min-w-[3rem] min-h-[3rem] w-12 h-12 border flex items-center justify-center overflow-hidden rounded-full">
										<img src={u.profilePicture} alt={u.name} />
									</div>
									<div className="flex flex-col">
										<span className="font-semibold">{getUserName(u)}</span>
										<span className="opacity-80 text-sm">{USER_TYPE_LABELS[u.type]}</span>
									</div>
								</div>

								<div className="flex flex-col gap-1">
									<span className="flex items-center gap-2">
										<Tooltip tooltip="E-mail address">
											<BsEnvelopeFill />
										</Tooltip>
										{u.email}
									</span>
									<span className="flex items-center gap-2">
										<Tooltip tooltip="Expiration Date">
											<BsStopwatchFill />
										</Tooltip>
										{u.subscriptionExpirationDate ? moment(u.subscriptionExpirationDate).format("DD/MM/YYYY") : "Unlimited"}
									</span>
									<span className="flex items-center gap-2">
										<Tooltip tooltip="Last Login">
											<BsClockFill />
										</Tooltip>
										{u.lastLogin ? moment(u.lastLogin).format("DD/MM/YYYY - HH:mm") : "N/A"}
									</span>
								</div>
							</button>
						))}
					</section>
				</Layout>
			)}
		</>
	);
}
