"use client";

import { useEffect } from "react";

interface Props {
  open: boolean;
  busy?: boolean;
  onCancel: () => void;
  onConfirm: () => void;
}

// Shared by the customer (/profile-settings) and astrologer (/astrologer/profile)
// "Delete account" actions. Reuses the `.leave-consult-*` popup styles.
export default function DeleteAccountModal({ open, busy = false, onCancel, onConfirm }: Props) {
  useEffect(() => {
    if (!open) return;

    const onKey = (event: KeyboardEvent) => {
      if (event.key === "Escape" && !busy) onCancel();
    };

    window.addEventListener("keydown", onKey);

    return () => window.removeEventListener("keydown", onKey);
  }, [open, busy, onCancel]);

  if (!open) return null;

  return (
    <div
      className="leave-consult-backdrop"
      role="dialog"
      aria-modal="true"
      aria-labelledby="delete-account-title"
      onClick={() => {
        if (!busy) onCancel();
      }}
    >
      <div className="leave-consult-modal" onClick={(event) => event.stopPropagation()}>
        <h5 id="delete-account-title">Delete account</h5>

        <p>
          Are you sure you want to delete your account? Once your account is permanently deleted,
          you will not be able to log in or sign up again using the same mobile number.
        </p>

        <div className="leave-consult-actions">
          <button type="button" className="leave-consult-continue" onClick={onCancel} disabled={busy}>
            Cancel
          </button>

          <button type="button" className="leave-consult-end" onClick={onConfirm} disabled={busy}>
            {busy ? "Deleting..." : "Delete account"}
          </button>
        </div>
      </div>
    </div>
  );
}
