import React, { useEffect } from "react";

import { useRouter } from "next/router";
import Module from "@Front/Config/Module";
import UserStore from "@Front/Stores/UserStore";
import { IBlock } from "@Front/Components/DesignSystem/SearchBlockList/BlockList/Block";
import DefaultDashboardWithList, { IPropsDashboardWithList } from "../DefaultDashboardWithList";
import User from "le-coffre-resources/dist/Notary";

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

type IProps = IPropsDashboardWithList;

export default function DefaultCollaboratorDashboard(props: IProps) {
	const [collaborators, setCollaborators] = React.useState<User[] | null>(null);
	const router = useRouter();
	const { collaboratorUid } = router.query;
	useEffect(() => {
		/* TODO: review
		const jwt = JwtService.getInstance().decodeJwt();
		if (!jwt) return;
		const query: IGetUsersparams = {
			where: { office_uid: jwt.office_Id },
			include: {
				contact: true,
				seats: {
					include: {
						subscription: true,
					},
				},
			},
		};
		Users.getInstance()
			.get(query)
			.then((users) => setCollaborators(users));
			*/

		const user: any = UserStore.instance.getUser();
		const officeId: string = user.office.uid;

		CollaboratorService.getCollaborators((processes: any[]) => {
			if (processes.length > 0) {
				let collaborators: any[] = processes.map((process: any) => process.processData);

				// FilterBy office.uid
				collaborators = collaborators.filter((collaborator: any) => collaborator.office.uid === officeId);

				setCollaborators(collaborators);
			}
		});
	}, []);

	const onSelectedBlock = (block: IBlock) => {
		router.push(
			Module.getInstance().get().modules.pages.Collaborators.pages.CollaboratorInformations.props.path.replace("[uid]", block.id),
		);
	};

	return (
		<DefaultDashboardWithList
			{...props}
			onSelectedBlock={onSelectedBlock}
			blocks={
				collaborators
					? collaborators.map((collaborator) => ({
						id: collaborator.uid!,
						primaryText: collaborator.contact?.first_name + " " + collaborator.contact?.last_name,
						isActive: collaborator.uid === collaboratorUid,
					}))
					: []
			}
		/>
	);
}
