import { AppRuleActions, AppRuleNames } from "@Front/Api/Entities/rule";
import Rules, { RulesMode } from "@Front/Components/Elements/Rules";
import Module from "@Front/Config/Module";
import Toasts from "@Front/Stores/Toasts";
import { AdjustmentsVerticalIcon, BanknotesIcon, Square3Stack3DIcon, TagIcon, UsersIcon } from "@heroicons/react/24/outline";
import { usePathname } from "next/navigation";
import React, { useCallback, useEffect } from "react";

import Menu from "../../Menu";
import { IItem } from "../../Menu/MenuItem";
import ButtonHeader from "../ButtonHeader";
import classes from "./classes.module.scss";

export default function Navigation() {
	const pathname = usePathname();

	const getAnchoringStatus = useCallback(async () => {
		/* TODO: review
		const anchors = await OfficeFolderAnchors.getInstance().get({
			where: {
				status: {
					not: "VERIFIED_ON_CHAIN",
				},
			},
			include: {
				folder: true,
			},
		});
		*/
		// const anchors = [] as any[];

		/* TODO: review
		try {
			for (const anchor of anchors) {
				await OfficeFolderAnchors.getInstance().getByUid(anchor.folder?.uid as string);
			}
		} catch (e) {
			console.error(e);
		}
		*/
	}, []);

	const getNotifications = useCallback(async () => {
		//await getAnchoringStatus();

		/* TODO: review
		const notifications = await Notifications.getInstance().get({
			where: {
				read: false,
			},
			include: {
				notification: true,
			},
			orderBy: {
				notification: { created_at: "desc" },
			},
		});
		*/
		const notifications = [] as any[];

		notifications.forEach((notification) => {
			Toasts.getInstance().open({
				title: notification.notification.message,
				uid: notification.uid,
				redirectUrl: notification.notification.redirection_url,
				created_at: notification.notification.created_at,
			});
		});
	}, []);

	useEffect(() => {
		getAnchoringStatus();
		getNotifications();
	}, [pathname, getNotifications, getAnchoringStatus]);

	return (
		<div className={classes["root"]}>
			<Rules
				mode={RulesMode.OPTIONAL}
				rules={[
					{
						action: AppRuleActions.read,
						name: AppRuleNames.officeFolders,
					},
				]}>
				<>
					<ButtonHeader
						text={"Dossiers en cours"}
						path={Module.getInstance().get().modules.pages.Folder.props.path}
						routesActive={[
							Module.getInstance().get().modules.pages.Folder.pages.FolderInformation.props.path,
							Module.getInstance().get().modules.pages.Folder.pages.CreateFolder.props.path,
						]}
					/>
					<ButtonHeader
						text={"Dossiers archivés"}
						path={Module.getInstance().get().modules.pages.Folder.pages.FolderArchived.props.path}
						routesActive={[Module.getInstance().get().modules.pages.Folder.pages.FolderArchived.props.path]}
					/>
					<Menu items={officeItems} openingSide="center" openOnHover>
						<ButtonHeader
							text={"Espace office"}
							path={Module.getInstance().get().modules.pages.Collaborators.props.path}
							routesActive={officeItems.flatMap((item) => item.routesActive ?? [])}
							disabledLink
						/>
					</Menu>
					<Rules
						mode={RulesMode.NECESSARY}
						rules={[
							{
								action: AppRuleActions.update,
								name: AppRuleNames.officeRoles,
							},
						]}>
						<Menu items={superAdminItems} openingSide="center" openOnHover>
							<ButtonHeader
								text={"Espace super admin"}
								path={Module.getInstance().get().modules.pages.Collaborators.props.path}
								routesActive={officeItems.flatMap((item) => item.routesActive ?? [])}
								disabledLink
							/>
						</Menu>
					</Rules>
				</>
			</Rules>
		</div>
	);
}

const officeItems: IItem[] = [
	{
		icon: <UsersIcon />,
		text: "Collaborateurs",
		link: Module.getInstance().get().modules.pages.Collaborators.props.path,
		routesActive: [
			Module.getInstance().get().modules.pages.Collaborators.pages.CollaboratorInformations.props.path,
			Module.getInstance().get().modules.pages.Collaborators.props.path,
		],
		rules: [
			{
				action: AppRuleActions.update,
				name: AppRuleNames.users,
			},
		],
	},
	{
		icon: <AdjustmentsVerticalIcon />,
		text: "Gestion des rôles",
		link: Module.getInstance().get().modules.pages.Roles.props.path,
		routesActive: [
			Module.getInstance().get().modules.pages.Roles.pages.Create.props.path,
			Module.getInstance().get().modules.pages.Roles.pages.RolesInformations.props.path,
			Module.getInstance().get().modules.pages.Roles.props.path,
		],
		rules: [
			{
				action: AppRuleActions.update,
				name: AppRuleNames.officeRoles,
			},
		],
	},
	{
		icon: <Square3Stack3DIcon />,
		text: "Paramétrage des listes de pièces",
		link: Module.getInstance().get().modules.pages.DeedTypes.props.path,
		routesActive: [
			Module.getInstance().get().modules.pages.DeedTypes.pages.Create.props.path,
			Module.getInstance().get().modules.pages.DeedTypes.pages.Edit.props.path,
			Module.getInstance().get().modules.pages.DeedTypes.props.path,
			Module.getInstance().get().modules.pages.DocumentTypes.props.path,
			Module.getInstance().get().modules.pages.DocumentTypes.pages.Create.props.path,
			Module.getInstance().get().modules.pages.DocumentTypes.pages.Edit.props.path,
			Module.getInstance().get().modules.pages.DocumentTypes.pages.DocumentTypesInformations.props.path,
		],
		rules: [
			{
				action: AppRuleActions.update,
				name: AppRuleNames.deedTypes,
			},
		],
	},
	{
		icon: <BanknotesIcon />,
		text: "RIB Office",
		link: Module.getInstance().get().modules.pages.OfficesRib.props.path,
		routesActive: [Module.getInstance().get().modules.pages.OfficesRib.props.path],
		rules: [
			{
				action: AppRuleActions.update,
				name: AppRuleNames.rib,
			},
		],
	},
	{
		icon: <TagIcon />,
		text: "Abonnement",
		link: Module.getInstance().get().modules.pages.Subscription.pages.Manage.props.path,
		routesActive: [
			Module.getInstance().get().modules.pages.Subscription.pages.Error.props.path,
			Module.getInstance().get().modules.pages.Subscription.pages.Success.props.path,
			Module.getInstance().get().modules.pages.Subscription.pages.Invite.props.path,
			Module.getInstance().get().modules.pages.Subscription.pages.Manage.props.path,
			Module.getInstance().get().modules.pages.Subscription.pages.ManageCollaborators.props.path,
			Module.getInstance().get().modules.pages.Subscription.pages.New.props.path,
			Module.getInstance().get().modules.pages.Subscription.pages.Subscribe.props.path,
		],
		rules: [
			{
				action: AppRuleActions.update,
				name: AppRuleNames.subscriptions,
			},
		],
	},
];

const superAdminItems: IItem[] = [
	{
		icon: <UsersIcon />,
		text: "Gestion des utilisateurs",
		link: Module.getInstance().get().modules.pages.Users.props.path,
		routesActive: [
			Module.getInstance().get().modules.pages.Users.pages.UsersInformations.props.path,
			Module.getInstance().get().modules.pages.Users.props.path,
		],
		rules: [
			{
				action: AppRuleActions.update,
				name: AppRuleNames.offices,
			},
		],
	},
	{
		icon: <UsersIcon />,
		text: "Gestion des offices",
		link: Module.getInstance().get().modules.pages.Offices.props.path,
		routesActive: [
			Module.getInstance().get().modules.pages.Offices.pages.OfficesInformations.props.path,
			Module.getInstance().get().modules.pages.Offices.props.path,
		],
		rules: [
			{
				action: AppRuleActions.update,
				name: AppRuleNames.offices,
			},
		],
	},
];
