"use client";

/**
 * Default flow: a plain anchor to `/api/auth/signout` (overridable via
 * the `signoutPath` prop). The route handler runs RP-initiated logout
 * in a single redirect — kills the NextAuth cookie, ends the Keycloak
 * SSO session, lands on the app's signed-out page.
 *
 * Apps with a different shape (no `/api/auth/signout`, need to fire
 * custom telemetry, etc.) pass `onSignOut` to replace the navigation
 * with a button + custom handler.
 */
type LogoutButtonProps = {
  label: string;
  signoutPath?: string;
  onSignOut?: () => void | Promise<void>;
};

export function LogoutButton({
  label,
  signoutPath = "/api/auth/signout",
  onSignOut,
}: LogoutButtonProps) {
  if (onSignOut) {
    return (
      <button
        type="button"
        onClick={() => {
          void onSignOut();
        }}
        className="dropdown-item"
      >
        <i className="ph-sign-out me-2"></i>
        {label}
      </button>
    );
  }

  return (
    <a href={signoutPath} className="dropdown-item">
      <i className="ph-sign-out me-2"></i>
      {label}
    </a>
  );
}
