import LogoIcon from "@Assets/logo_small_blue.svg";
import Folders from "@Front/Api/LeCoffreApi/Notary/Folders/Folders";
import Button, { EButtonVariant } from "@Front/Components/DesignSystem/Button";
import Separator, { ESeperatorColor, ESeperatorDirection } from "@Front/Components/DesignSystem/Separator";
import Typography, { ETypo, ETypoColor } from "@Front/Components/DesignSystem/Typography";
import HelpBox from "@Front/Components/Elements/HelpBox";
import DefaultNotaryDashboard from "@Front/Components/LayoutTemplates/DefaultNotaryDashboard";
import Module from "@Front/Config/Module";
import useUser from "@Front/Hooks/useUser";
import { DocumentIcon } from "@heroicons/react/24/outline";
import EFolderStatus from "le-coffre-resources/dist/Customer/EFolderStatus";
import Image from "next/image";
import Link from "next/link";
import { useRouter } from "next/router";
import { useEffect, useState } from "react";
import classes from "./classes.module.scss";

import FolderService from "src/common/Api/LeCoffreApi/sdk/FolderService";

export default function Folder() {
	const [_isArchivedModalOpen, _setIsArchivedModalOpen] = useState(true);
	const router = useRouter();

	const { user: activeUser } = useUser();

	useEffect(() => {
		// TODO: review
		FolderService.getFoldersBy({ status: EFolderStatus.LIVE }).then((processes: Record<string, any>) => {	
			if (Object.keys(processes).length > 0) {
				let folders: any[] = Object.entries(processes).map(([processId, process]) => {
					const res = {
						...process,
						processId: processId
					};
					return res;
				});

				// OrderBy created_at desc
				folders = folders.sort((a: any, b: any) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime());

				if (folders.length > 0) {
					const folderUid = folders[0]?.processId;

					if (!folderUid) {
						return;
					}
					
					router.push(
						Module.getInstance()
							.get()
							.modules.pages.Folder.pages.FolderInformation.props.path.replace("[folderUid]", folderUid as string)
					);
				}
			} else {
				console.debug('[Folder] No folders found');
			}
		});
	}, [router]);
	return (
		<DefaultNotaryDashboard title={"Dossier"} mobileBackText={"Liste des dossiers"}>
			<div className={classes["root"]}>
				<div className={classes["content"]}>
					<div className={classes["title-container"]}>
						<Image src={LogoIcon} alt="logo" />

						{activeUser && activeUser.contact && (
							<Typography typo={ETypo.TITLE_H1} color={ETypoColor.TEXT_ACCENT}>
								Bonjour {activeUser.contact.first_name}, bienvenue sur LeCoffre.io
							</Typography>
						)}
						{!activeUser ||
							(!activeUser.contact && (
								<Typography typo={ETypo.TITLE_H1} color={ETypoColor.TEXT_ACCENT}>
									Bonjour, bienvenue sur LeCoffre.io
								</Typography>
							))}
						<Typography typo={ETypo.TEXT_LG_REGULAR}>
							Commencez par créer votre{" "}
							<Typography typo={ETypo.TEXT_LG_SEMIBOLD} type="span">
								premier dossier
							</Typography>{" "}
							pour profiter de toutes les fonctionnalités de notre plateforme sécurisée.
						</Typography>
					</div>

					<div className={classes["no-folder"]}>
						<DocumentIcon width={32} />
						<div className={classes["text-container"]}>
							<Typography typo={ETypo.TEXT_LG_SEMIBOLD}>Aucun dossier créé</Typography>
							<Typography typo={ETypo.TEXT_MD_REGULAR}>
								Vous n'avez pas encore de dossier créé. En quelques clics, commencez un nouveau dossier pour organiser et
								sécuriser vos documents notariés.
							</Typography>
						</div>
						<Link href={Module.getInstance().get().modules.pages.Folder.pages.CreateFolder.props.path}>
							<Button variant={EButtonVariant.INFO}>Créer votre premier dossier</Button>
						</Link>
					</div>

					<div className={classes["help-container"]}>
						<HelpBox
							title="Besoin d'aide ?"
							description="Consultez nos guides pour bien démarrer."
							button={{ text: "Accéder aux guides" }}
						/>

						<Separator
							className={classes["desktop"]}
							direction={ESeperatorDirection.VERTICAL}
							size={128}
							color={ESeperatorColor.LIGHT}
						/>
						<Separator className={classes["mobile"]} direction={ESeperatorDirection.HORIZONTAL} color={ESeperatorColor.LIGHT} />

						<HelpBox
							title="Vous avez des questions ?"
							description="Notre équipe de support est là pour vous aider."
							button={{ text: "Contactez le support" }}
						/>
					</div>
				</div>
			</div>
		</DefaultNotaryDashboard>
	);
}
