/* eslint-disable @next/next/no-img-element */
import Head from "next/head";
import {withIronSessionSsr} from "iron-session/next";
import {sessionOptions} from "@/lib/session";
import useUser from "@/hooks/useUser";
import {ToastContainer} from "react-toastify";
import Layout from "@/components/High/Layout";
import CodeGenerator from "./(admin)/CodeGenerator";
import ExamLoader from "./(admin)/ExamLoader";
import {Tab} from "@headlessui/react";
import clsx from "clsx";
import Lists from "./(admin)/Lists";
import BatchCodeGenerator from "./(admin)/BatchCodeGenerator";
import {shouldRedirectHome} from "@/utils/navigation.disabled";
import ExamGenerator from "./(admin)/ExamGenerator";
import BatchCreateUser from "./(admin)/BatchCreateUser";
import {checkAccess, getTypesOfUser} from "@/utils/permissions";
import usePermissions from "@/hooks/usePermissions";
import {useState} from "react";
import Modal from "@/components/Modal";
import IconCard from "@/dashboards/IconCard";
import {BsCode, BsCodeSquare, BsGearFill, BsPeopleFill, BsPersonFill} from "react-icons/bs";
import UserCreator from "./(admin)/UserCreator";
import CorporateGradingSystem from "./(admin)/CorporateGradingSystem";
import useGradingSystem from "@/hooks/useGrading";
import {CEFR_STEPS} from "@/resources/grading";

export const getServerSideProps = withIronSessionSsr(({req, res}) => {
	const user = req.session.user;
	if (!user || !user.isVerified) {
		return {
			redirect: {
				destination: "/login",
				permanent: false,
			},
		};
	}

	if (shouldRedirectHome(user) || !checkAccess(user, ["admin", "developer", "corporate", "teacher", "mastercorporate"])) {
		return {
			redirect: {
				destination: "/",
				permanent: false,
			},
		};
	}

	return {
		props: {user: req.session.user},
	};
}, sessionOptions);

export default function Admin() {
	const {user} = useUser({redirectTo: "/login"});
	const {permissions} = usePermissions(user?.id || "");
	const {gradingSystem, mutate} = useGradingSystem();

	const [modalOpen, setModalOpen] = useState<string>();

	return (
		<>
			<Head>
				<title>Settings Panel | 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} className="gap-6">
					<Modal isOpen={modalOpen === "batchCreateUser"} onClose={() => setModalOpen(undefined)}>
						<BatchCreateUser user={user} />
					</Modal>
					<Modal isOpen={modalOpen === "batchCreateCode"} onClose={() => setModalOpen(undefined)}>
						<CodeGenerator user={user} />
					</Modal>
					<Modal isOpen={modalOpen === "createCode"} onClose={() => setModalOpen(undefined)}>
						<BatchCodeGenerator user={user} />
					</Modal>
					<Modal isOpen={modalOpen === "createUser"} onClose={() => setModalOpen(undefined)}>
						<UserCreator user={user} />
					</Modal>
					<Modal isOpen={modalOpen === "gradingSystem"} onClose={() => setModalOpen(undefined)}>
						<CorporateGradingSystem
							user={user}
							defaultSteps={gradingSystem?.steps || CEFR_STEPS}
							mutate={(steps) => mutate({user: user.id, steps})}
						/>
					</Modal>

					<section className="w-full grid grid-cols-2 -md:grid-cols-1 gap-8">
						<ExamLoader />
						{checkAccess(user, getTypesOfUser(["teacher"]), permissions, "viewCodes") && (
							<div className="w-full grid grid-cols-2 gap-4">
								<IconCard
									Icon={BsCode}
									label="Generate Single Code"
									color="purple"
									className="w-full h-full"
									onClick={() => setModalOpen("createCode")}
								/>
								<IconCard
									Icon={BsCodeSquare}
									label="Generate Codes in Batch"
									color="purple"
									className="w-full h-full"
									onClick={() => setModalOpen("batchCreateCode")}
								/>
								<IconCard
									Icon={BsPersonFill}
									label="Create Single User"
									color="purple"
									className="w-full h-full"
									onClick={() => setModalOpen("createUser")}
								/>
								<IconCard
									Icon={BsPeopleFill}
									label="Create Users in Batch"
									color="purple"
									className="w-full h-full"
									onClick={() => setModalOpen("batchCreateUser")}
								/>
								{checkAccess(user, ["admin", "corporate", "developer", "mastercorporate"]) && (
									<IconCard
										Icon={BsGearFill}
										label="Grading System"
										color="purple"
										className="w-full h-full col-span-2"
										onClick={() => setModalOpen("gradingSystem")}
									/>
								)}
							</div>
						)}
					</section>
					<section className="w-full">
						<Lists user={user} />
					</section>
				</Layout>
			)}
		</>
	);
}
