/* eslint-disable @next/next/no-img-element */
import Head from "next/head";
import Navbar from "@/components/Navbar";
import {BsFileEarmarkText, BsPencil, BsStar, BsBook, BsHeadphones, BsPen, BsMegaphone} from "react-icons/bs";
import {withIronSessionSsr} from "iron-session/next";
import {sessionOptions} from "@/lib/session";
import {useEffect, useState} from "react";
import useStats from "@/hooks/useStats";
import {averageScore, groupBySession, totalExams} from "@/utils/stats";
import useUser from "@/hooks/useUser";
import Sidebar from "@/components/Sidebar";
import Diagnostic from "@/components/Diagnostic";
import {ToastContainer} from "react-toastify";
import {capitalize} from "lodash";
import {Module} from "@/interfaces";
import ProgressBar from "@/components/Low/ProgressBar";
import Layout from "@/components/High/Layout";
import {calculateAverageLevel} from "@/utils/score";
import axios from "axios";
import DemographicInformationInput from "@/components/DemographicInformationInput";
import moment from "moment";
import Link from "next/link";
import {MODULE_ARRAY} from "@/utils/moduleUtils";
import ProfileSummary from "@/components/ProfileSummary";
import StudentDashboard from "@/dashboards/Student";

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

	if (!user || !user.isVerified) {
		res.setHeader("location", "/login");
		res.statusCode = 302;
		res.end();
		return {
			props: {
				user: null,
			},
		};
	}

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

export default function Home() {
	const [showDiagnostics, setShowDiagnostics] = useState(false);
	const [showDemographicInput, setShowDemographicInput] = useState(false);
	const {user, mutateUser} = useUser({redirectTo: "/login"});

	useEffect(() => {
		if (user) {
			setShowDemographicInput(!user.demographicInformation);
			setShowDiagnostics(user.isFirstLogin);
		}
	}, [user]);

	const checkIfUserExpired = () => {
		const expirationDate = user!.subscriptionExpirationDate;

		if (expirationDate === null || expirationDate === undefined) return false;
		if (moment(expirationDate).isAfter(moment(new Date()))) return false;

		return true;
	};

	if (user && (user.isDisabled || checkIfUserExpired())) {
		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>
				<Layout user={user} navDisabled>
					<div className="flex flex-col items-center justify-center text-center w-full gap-4">
						{user.isDisabled ? (
							<>
								<span className="font-bold text-lg">Your account has been disabled!</span>
								<span>Please contact an administrator if you believe this to be a mistake.</span>
							</>
						) : (
							<>
								<span className="font-bold text-lg">Your subscription has expired!</span>
								<div className="flex flex-col items-center">
									<span>
										Please purchase a new time pack{" "}
										<Link
											className="font-bold text-mti-purple-light underline hover:text-mti-purple-dark transition ease-in-out duration-300"
											href="https://encoach.com/join">
											here
										</Link>
										.
									</span>
									<span className="max-w-md">
										If you are not the one in charge of your subscription, please contact the one responsible to extend it.
									</span>
								</div>
							</>
						)}
					</div>
				</Layout>
			</>
		);
	}

	if (user && showDemographicInput) {
		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>
				<Layout user={user} navDisabled>
					<DemographicInformationInput mutateUser={mutateUser} />
				</Layout>
			</>
		);
	}

	if (user && showDiagnostics) {
		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>
				<Layout user={user} navDisabled>
					<Diagnostic user={user} onFinish={() => setShowDiagnostics(false)} />
				</Layout>
			</>
		);
	}

	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 />
			{user && (
				<Layout user={user}>
					{user.type === "student" && <StudentDashboard user={user} />}
					{user.type === "teacher" && <StudentDashboard user={user} />}
					{user.type === "admin" && <StudentDashboard user={user} />}
					{user.type === "owner" && <StudentDashboard user={user} />}
					{user.type === "developer" && <StudentDashboard user={user} />}
				</Layout>
			)}
		</>
	);
}
