"use client";

import React from "react";
import { UserPlus } from "lucide-react";

import {
	Dialog,
	DialogContent,
	DialogDescription,
	DialogHeader,
	DialogTitle,
	DialogTrigger,
} from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";

import CreateUserForm from "@/components/admin/users/create-user/CreateUserForm";
import { useCreateUserDialog } from "@/components/admin/users/create-user/useCreateUserDialog";

export default function CreateUserDialog({ disabled = false, onCreated }) {
	const {
		open,
		setOpen,
		form,
		setPatch,
		error,
		policyLines,
		branchesStatus,
		branchExistence,
		isSubmitting,
		effectiveDisabled,
		canSubmit,
		handleSubmit,
		handleOpenChange,
	} = useCreateUserDialog({ disabled, onCreated });

	return (
		<Dialog open={open} onOpenChange={handleOpenChange}>
			<DialogTrigger asChild>
				<Button type="button" disabled={disabled} title="Benutzer anlegen">
					<UserPlus className="h-4 w-4" />
					Benutzer anlegen
				</Button>
			</DialogTrigger>

			<DialogContent className="sm:max-w-xl">
				<DialogHeader>
					<DialogTitle>Benutzer anlegen</DialogTitle>
					<DialogDescription>
						Neues Benutzerkonto erstellen. Der Benutzer muss das Passwort beim
						ersten Login ändern.
					</DialogDescription>
				</DialogHeader>

				<CreateUserForm
					form={form}
					setPatch={setPatch}
					error={error}
					policyLines={policyLines}
					branchesStatus={branchesStatus}
					branchExistence={branchExistence}
					isSubmitting={isSubmitting}
					disabled={effectiveDisabled}
					canSubmit={canSubmit}
					onCancel={() => setOpen(false)}
					onSubmit={handleSubmit}
				/>
			</DialogContent>
		</Dialog>
	);
}
