import Folders from "@Front/Api/LeCoffreApi/Customer/Folders/Folders";
import BlockList, { IBlock } from "@Front/Components/DesignSystem/BlockList";
import Typography, { ITypo } from "@Front/Components/DesignSystem/Typography";
import DefaultDoubleSidePage from "@Front/Components/LayoutTemplates/DefaultDoubleSidePage";
import JwtService from "@Front/Services/JwtService/JwtService";
import { OfficeFolder } from "le-coffre-resources/dist/Customer";
import { useRouter } from "next/router";
import { useCallback, useEffect, useState } from "react";

import LandingImage from "../Login/landing-connect.jpeg";
import classes from "./classes.module.scss";

export default function SelectFolder() {
	const [folders, setFolders] = useState<OfficeFolder[]>([]);
	const router = useRouter();

	useEffect(() => {
		async function getFolders() {
			const jwt = JwtService.getInstance().decodeCustomerJwt();
			if (!jwt) return;
			console.log(jwt);

			const folders = await Folders.getInstance().get({
				q: {
					where: {
						customers: {
							some: {
								uid: jwt.customerId || (jwt as any).customerId,
							},
						},
					},
				},
			});
			setFolders(folders);
		}

		getFolders();
	}, []);

	const handleSelectBlock = useCallback(
		(folder: IBlock) => {
			router.push("/client-dashboard/" + folder.id);
		},
		[router],
	);

	return (
		<DefaultDoubleSidePage title="Sélectionner un dossier" image={LandingImage}>
			<div className={classes["root"]}>
				<div className={classes["select-folder-container"]}>
					<div className={classes["title"]}>
						<Typography typo={ITypo.H1}>Vos dossiers</Typography>
					</div>
					<div className={classes["folders-container"]}>
						<BlockList
							onSelectedBlock={handleSelectBlock}
							blocks={folders.map((folder) => {
								return {
									id: folder.uid!,
									name: folder.name!,
									selected: false,
								};
							})}
						/>
					</div>
				</div>
			</div>
		</DefaultDoubleSidePage>
	);
}
