/* eslint-disable @next/next/no-img-element */
import Head from "next/head";
import {useEffect, useState} from "react";
import {withIronSessionSsr} from "iron-session/next";
import {sessionOptions} from "@/lib/session";
import {shouldRedirectHome} from "@/utils/navigation.disabled";
import {Permission, PermissionType} from "@/interfaces/permissions";
import {getPermissionDoc} from "@/utils/permissions.be";
import {User} from "@/interfaces/user";
import Layout from "@/components/High/Layout";
import {getUsers} from "@/utils/users.be";
import {BsTrash} from "react-icons/bs";
import Select from "@/components/Low/Select";
import Button from "@/components/Low/Button";
import axios from "axios";
import {toast, ToastContainer} from "react-toastify";
import {Type as UserType} from "@/interfaces/user";
import {getGroups} from "@/utils/groups.be";
interface BasicUser {
	id: string;
	name: string;
	type: UserType;
}

interface PermissionWithBasicUsers {
	id: string;
	type: PermissionType;
	users: BasicUser[];
}

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

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

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

	if (!params?.id) {
		return {
			redirect: {
				destination: "/permissions",
				permanent: false,
			},
		};
	}

	// Fetch data from external API
	const permission: Permission = await getPermissionDoc(params.id as string);

	const allUserData: User[] = await getUsers();
	const groups = await getGroups();

	const userGroups = groups.filter((x) => x.admin === user.id);
	const filteredGroups =
		user.type === "corporate"
			? userGroups
			: user.type === "mastercorporate"
			? groups.filter((x) => userGroups.flatMap((y) => y.participants).includes(x.admin))
			: groups;

	const users = allUserData.map((u) => ({
		id: u.id,
		name: u.name,
		type: u.type,
	})) as BasicUser[];

	const filteredUsers = ["mastercorporate", "corporate"].includes(user.type)
		? users.filter((u) => filteredGroups.flatMap((g) => g.participants).includes(u.id))
		: users;

	// const res = await fetch("api/permissions");
	// const permissions: Permission[] = await res.json();
	// Pass data to the page via props
	const usersData: BasicUser[] = permission.users.reduce((acc: BasicUser[], userId) => {
		const user = filteredUsers.find((u) => u.id === userId) as BasicUser;
		if (!!user) acc.push(user);
		return acc;
	}, []);

	return {
		props: {
			// permissions: permissions.map((p) => ({ id: p.id, type: p.type })),
			permission: {
				...permission,
				id: params.id,
				users: usersData,
			},
			user: req.session.user,
			users: filteredUsers,
		},
	};
}, sessionOptions);

interface Props {
	permission: PermissionWithBasicUsers;
	user: User;
	users: BasicUser[];
}

export default function Page(props: Props) {
	const {permission, user, users} = props;

	const [selectedUsers, setSelectedUsers] = useState<string[]>(() => permission.users.map((u) => u.id));

	const onChange = (value: any) => {
		setSelectedUsers((prev) => {
			if (value?.value) {
				return [...prev, value?.value];
			}
			return prev;
		});
	};
	const removeUser = (id: string) => {
		setSelectedUsers((prev) => prev.filter((u) => u !== id));
	};

	const update = async () => {
		try {
			await axios.patch(`/api/permissions/${permission.id}`, {
				users: selectedUsers,
			});
			toast.success("Permission updated");
		} catch (err) {
			toast.error("Failed to update permission");
		}
	};

	return (
		<>
			<Head>
				<title>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 />
			<Layout user={user} className="gap-6">
				<div className="flex flex-col gap-6 w-full h-[88vh] overflow-y-scroll scrollbar-hide rounded-xl">
					<h1 className="text-2xl font-semibold">Permission: {permission.type as string}</h1>
					<div className="flex gap-3">
						<Select
							value={null}
							options={users
								.filter((u) => !selectedUsers.includes(u.id))
								.map((u) => ({
									label: `${u?.type}-${u?.name}`,
									value: u.id,
								}))}
							onChange={onChange}
						/>
						<Button onClick={update}>Update</Button>
					</div>
					<div className="flex flex-row justify-between">
						<div className="flex flex-col gap-3">
							<h2>Blacklisted Users</h2>
							<div className="flex gap-3 flex-wrap">
								{selectedUsers.map((userId) => {
									const user = users.find((u) => u.id === userId);
									return (
										<div className="flex p-4 rounded-xl w-auto bg-mti-purple-light text-white gap-4" key={userId}>
											<span className="text-base first-letter:uppercase">
												{user?.type}-{user?.name}
											</span>
											<BsTrash style={{cursor: "pointer"}} onClick={() => removeUser(userId)} size={20} />
										</div>
									);
								})}
							</div>
						</div>
						<div className="flex flex-col gap-3">
							<h2>Whitelisted Users</h2>
							<div className="flex flex-col gap-3 flex-wrap">
								{users
									.filter((user) => !selectedUsers.includes(user.id))
									.map((user) => {
										return (
											<div className="flex p-4 rounded-xl w-auto bg-mti-purple-light text-white gap-4" key={user.id}>
												<span className="text-base first-letter:uppercase">
													{user?.type}-{user?.name}
												</span>
											</div>
										);
									})}
							</div>
						</div>
					</div>
				</div>
			</Layout>
		</>
	);
}
