import Module from "@Front/Config/Module";
import React, { useCallback, useEffect } from "react";

import HeaderLink from "../HeaderLink";
import classes from "./classes.module.scss";
import Rules, { RulesMode } from "@Front/Components/Elements/Rules";
import { AppRuleActions, AppRuleNames } from "@Front/Api/Entities/rule";
import { usePathname } from "next/navigation";
import Notifications from "@Front/Api/LeCoffreApi/Notary/Notifications/Notifications";
import Toasts from "@Front/Stores/Toasts";
import OfficeFolderAnchors from "@Front/Api/LeCoffreApi/Notary/OfficeFolderAnchors/OfficeFolderAnchors";
import HeaderSubmenu from "../HeaderSubmenu";
export default function Navigation() {
	const pathname = usePathname();

	const getAnchoringStatus = useCallback(async () => {
		const anchors = await OfficeFolderAnchors.getInstance().get({
			where: {
				status: {
					not: "VERIFIED_ON_CHAIN",
				},
			},
			include: {
				folder: true,
			},
		});

		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();
		const notifications = await Notifications.getInstance().get({
			where: {
				read: false,
			},
			include: {
				notification: true,
			},
			orderBy: {
				notification: { created_at: "desc" },
			},
		});
		notifications.forEach((notification) => {
			Toasts.getInstance().open({
				title: notification.notification.message,
				uid: notification.uid,
				redirectUrl: notification.notification.redirection_url,
			});
		});
	}, []);

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

	return (
		<div className={classes["root"]}>
			<HeaderLink
				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,
				]}
			/>
			<HeaderLink
				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]}
			/>
			<Rules
				mode={RulesMode.NECESSARY}
				rules={[
					{
						action: AppRuleActions.update,
						name: AppRuleNames.officeRoles,
					},
				]}>
				<HeaderSubmenu
					text={"Espace office"}
					links={[
						{
							text: "Collaborateurs",
							path: 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.read,
									name: AppRuleNames.users,
								},
							],
						},
						{
							text: "Gestion des rôles",
							path: 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,
								},
							],
						},
						{
							text: "Paramétrage des listes de pièces",
							path: 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,
								},
							],
						},
						{
							text: "RIB Office",
							path: Module.getInstance().get().modules.pages.OfficesRib.props.path,
							rules: [
								{
									action: AppRuleActions.update,
									name: AppRuleNames.rib,
								},
							],
						},
						{
							text: "Abonnement",
							path: 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>
			<Rules
				mode={RulesMode.NECESSARY}
				rules={[
					{
						action: AppRuleActions.update,
						name: AppRuleNames.officeRoles,
					},
				]}>
				<HeaderSubmenu
					text={"Espace super admin"}
					links={[
						{
							text: "Gestion des utilisateurs",
							path: 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,
								},
							],
						},
						{
							text: "Gestion des offices",
							path: 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,
								},
							],
						},
					]}
				/>
			</Rules>
		</div>
	);
}
