import { IBlock } from "@Front/Components/DesignSystem/SearchBlockList/BlockList/Block";
import Module from "@Front/Config/Module";
import JwtService from "@Front/Services/JwtService/JwtService";
import { OfficeFolder } from "le-coffre-resources/dist/Customer";
import { useRouter } from "next/router";
import React, { useEffect, useState } from "react";

import DefaultDashboardWithList, { IPropsDashboardWithList } from "../DefaultDashboardWithList";

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

type IProps = IPropsDashboardWithList & {};

export default function DefaultCustomerDashboard(props: IProps) {
	const router = useRouter();
	const { folderUid, profileUid } = router.query;
	const [folders, setFolders] = useState<OfficeFolder[]>([]);

	useEffect(() => {
		const jwt = JwtService.getInstance().decodeCustomerJwt();
		if (!jwt) return;

		/*
		Folders.getInstance()
			.get({
				q: {
					where: {
						customers: {
							some: {
								contact: {
									email: jwt.email,
								},
							},
						},
					},
					orderBy: [
						{
							created_at: "desc",
						},
					],
					include: {
						customers: true,
					},
				},
			})
			.then((folders) => setFolders(folders));
			*/

		FolderService.getFolders().then((processes: any[]) => {
			if (processes.length > 0) {
				const folders: any[] = processes.map((process: any) => process.processData);
				setFolders(folders);
			}
		});
	}, []);

	const onSelectedBlock = (block: IBlock) => {
		const folder = folders.find((folder) => folder.uid === block.id);
		if (!folder) return;
		router.push(
			Module.getInstance()
				.get()
				.modules.pages.ClientDashboard.props.path
				.replace("[folderUid]", folder.uid ?? "")
				.replace("[profileUid]", profileUid as string ?? ""),
		);
	};
	return <DefaultDashboardWithList {...props} onSelectedBlock={onSelectedBlock} blocks={getBlocks(folders)} headerConnected={false} />;

	function getBlocks(folders: OfficeFolder[]): IBlock[] {
		return folders.map((folder) => {
			return {
				id: folder.uid!,
				primaryText: folder.name!,
				secondaryText: folder.folder_number!,
				isActive: folderUid === folder.uid,
			};
		});
	}
}
