import Dialog from '~/components/Dialog';
import { Machine, User } from '~/types';

interface DeleteProps {
	user: User & { machines: Machine[] };
	isOpen: boolean;
	setIsOpen: (isOpen: boolean) => void;
}

export default function DeleteUser({ user, isOpen, setIsOpen }: DeleteProps) {
	const name = user.name || user.displayName;

	return (
		<Dialog isOpen={isOpen} onOpenChange={setIsOpen}>
			<Dialog.Panel
				variant={user.machines.length > 0 ? 'unactionable' : 'normal'}
			>
				<Dialog.Title>Delete {name}?</Dialog.Title>
				{user.machines.length > 0 ? (
					<Dialog.Text className="mb-6">
						Users cannot be deleted if they have machines. Please delete or
						re-assign their machines to other users before proceeding.
					</Dialog.Text>
				) : (
					<Dialog.Text className="mb-6">
						Deleted users cannot be recovered.
						{user.provider === 'oidc' && (
							<p className="mt-4 text-sm text-headplane-600 dark:text-headplane-300">
								Since this user is authenticated via an external provider, they
								will be recreated if they sign in again.
							</p>
						)}
					</Dialog.Text>
				)}
				<input name="action_id" type="hidden" value="delete_user" />
				<input name="user_id" type="hidden" value={user.id} />
			</Dialog.Panel>
		</Dialog>
	);
}
