import {
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle,
} from "@/shared/common/ui/alert-dialog";
import React from "react";

export interface DynamicDialogProps {
  open: boolean; // Controlled open state of the dialog
  onOpenChange: (open: boolean) => void; // Callback when the open state changes
  title?: string; // Title text in the dialog header
  description?: string; // Description text in the dialog header
  confirmText?: string; // Text on the confirm button
  cancelText?: string; // Text on the cancel button
  onConfirm: (remark?: string) => void; // Action to run on confirmation with an optional remark
  onCancel?: () => void; // Action to run on cancel (optional)
  includeRemark?: boolean; // Whether to display the remark textarea
  remarkLabel?: string; // Label for the remark textarea
  remarkPlaceholder?: string; // Placeholder text for the remark textarea
}

export function DynamicDialog({
  open,
  onOpenChange,
  title = "Please confirm your action",
  description = "Are you sure you want to continue?",
  confirmText = "Confirm",
  cancelText = "Cancel",
  onConfirm,
  onCancel,
  includeRemark = false,
  remarkLabel = "Remark:",
  remarkPlaceholder = "Enter remark...",
}: DynamicDialogProps) {
  const [remark, setRemark] = React.useState<string>("");

  const handleConfirm = () => {
    onConfirm(includeRemark ? remark : undefined);
  };

  const handleCancel = () => {
    onCancel && onCancel();
    onOpenChange(false); // Close dialog after cancel
  };

  return (
    <AlertDialog open={open} onOpenChange={onOpenChange}>
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle>{title}</AlertDialogTitle>
          <AlertDialogDescription>{description}</AlertDialogDescription>
        </AlertDialogHeader>
        {includeRemark && (
          <div className="my-4 space-y-2">
            <label htmlFor="remark" className="block text-sm font-medium">
              {remarkLabel}
            </label>
            <textarea
              id="remark"
              placeholder={remarkPlaceholder}
              value={remark}
              onChange={(e) => setRemark(e.target.value)}
              className="mt-1 block w-full rounded-md border border-gray-300 shadow-sm focus:ring focus:ring-blue-500 focus:border-blue-500 sm:text-sm"
            />
          </div>
        )}
        <AlertDialogFooter>
          <AlertDialogCancel
            onClick={handleCancel}
            type="button"
            className="cursor-pointer"
          >
            {cancelText}
          </AlertDialogCancel>
          <AlertDialogAction
            onClick={handleConfirm}
            type="button"
            className="cursor-pointer"
          >
            {confirmText}
          </AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  );
}
