import Users, { IGetUsersparams } from "@Front/Api/LeCoffreApi/SuperAdmin/Users/Users";
import User from "le-coffre-resources/dist/SuperAdmin";
import React, { useEffect } from "react";

import { useRouter } from "next/router";
import Module from "@Front/Config/Module";
import { IBlock } from "@Front/Components/DesignSystem/SearchBlockList/BlockList/Block";
import DefaultDashboardWithList, { IPropsDashboardWithList } from "../DefaultDashboardWithList";

type IProps = IPropsDashboardWithList;

export default function DefaultUserDashboard(props: IProps) {
	const [users, setUsers] = React.useState<User[] | null>(null);
	const router = useRouter();
	const { userUid } = router.query;
	useEffect(() => {
		const query: IGetUsersparams = {
			include: { contact: true },
		};

		Users.getInstance()
			.get(query)
			.then((users) => setUsers(users));
	}, []);

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

	return (
		<DefaultDashboardWithList
			{...props}
			onSelectedBlock={onSelectedBlock}
			blocksList={
				users
					? users.map((user) => ({
							id: user.uid!,
							primaryText: user.contact?.first_name + " " + user.contact?.last_name,
							isActive: user.uid === userUid,
							secondaryText: user.contact?.email,
					  }))
					: []
			}
		/>
	);
}
