import { PersonIcon } from "@radix-ui/react-icons";
import { prisma, User } from "@repo/db";
import { AdminForm, ConnectionHistory, ProfileForm, UserReports } from "./_components/forms";
import { Error } from "../../../../_components/Error";

export default async function Page({ params }: { params: Promise<{ id: string }> }) {
	const { id } = await params;

	const user: User | null = await prisma.user.findUnique({
		where: {
			id: id,
		},
	});

	const dispoSessions = await prisma.connectedDispatcher.findMany({
		where: {
			userId: user?.id,
			logoutTime: {
				not: null,
			},
		},
		select: {
			loginTime: true,
			logoutTime: true,
		},
	});
	const totalDispoTime = dispoSessions.reduce((acc, session) => {
		const logoffTime = new Date(session.logoutTime!).getTime();
		const logonTime = new Date(session.loginTime).getTime();
		return acc + (logoffTime - logonTime);
	}, 0);

	const dispoTime = {
		hours: Math.floor(totalDispoTime / (1000 * 60 * 60)),
		minutes: Math.floor((totalDispoTime % (1000 * 60 * 60)) / (1000 * 60)),
		lastLogin: dispoSessions[dispoSessions.length - 1]?.loginTime,
	};

	const pilotSessions = await prisma.connectedAircraft.findMany({
		where: {
			userId: user?.id,
			logoutTime: {
				not: null,
			},
		},
		select: {
			loginTime: true,
			logoutTime: true,
			Station: true,
		},
	});

	const totalPilotTime = pilotSessions.reduce((acc, session) => {
		const logoffTime = new Date(session.logoutTime!).getTime();
		const logonTime = new Date(session.loginTime).getTime();
		return acc + (logoffTime - logonTime);
	}, 0);

	const pilotTime = {
		hours: Math.floor(totalPilotTime / (1000 * 60 * 60)),
		minutes: Math.floor((totalPilotTime % (1000 * 60 * 60)) / (1000 * 60)),
		lastLogin: pilotSessions[pilotSessions.length - 1]?.loginTime,
	};

	const totalReportsReports = await prisma.report.count({
		where: {
			reportedUserId: user?.id,
		},
	});
	const totalReports60Days = await prisma.report.count({
		where: {
			reportedUserId: user?.id,
			timestamp: {
				gte: new Date(Date.now() - 60 * 24 * 60 * 60 * 1000),
			},
		},
	});

	const totalReportsOpen = await prisma.report.count({
		where: {
			reportedUserId: user?.id,
			reviewed: false,
		},
	});

	const reports = {
		total: totalReportsReports,
		open: totalReportsOpen,
		total60Days: totalReports60Days,
	};

	if (!user) return <Error statusCode={404} title="User not found" />;
	return (
		<div className="grid grid-cols-6 gap-4">
			<div className="col-span-full">
				<p className="text-2xl font-semibold text-left flex items-center gap-2">
					<PersonIcon className="w-5 h-5" />
					{user?.firstname} {user?.lastname} #{user?.publicId}
				</p>
			</div>
			<div className="card bg-base-200 shadow-xl mb-4 col-span-6 xl:col-span-3">
				<ProfileForm user={user} />
			</div>
			<div className="card bg-base-200 shadow-xl mb-4 col-span-6 xl:col-span-3">
				<AdminForm user={user} dispoTime={dispoTime} pilotTime={pilotTime} reports={reports} />
			</div>
			<div className="card bg-base-200 shadow-xl mb-4 col-span-6 xl:col-span-6">
				<UserReports user={user} />
			</div>
			<div className="card bg-base-200 shadow-xl mb-4 col-span-6 xl:col-span-6">
				<ConnectionHistory user={user} />
			</div>
		</div>
	);
}
