// @design-system: overlays/AlertDialog
// Registered at /design-system#alertdialog-overlay - Phase 3 (Agent 3E) will render the gallery.

import * as RadixAlertDialog from '@radix-ui/react-alert-dialog';
import { cn } from '@/lib/cn';

// ─── Root + Trigger ────────────────────────────────────────────────────────

/** Root alert-dialog controller. */
export const AlertDialog = RadixAlertDialog.Root;

/** Trigger element. */
export const AlertDialogTrigger = RadixAlertDialog.Trigger;

/** Portal wrapper (internal use — not exported). */
const AlertDialogPortal = RadixAlertDialog.Portal;

// ─── Overlay ──────────────────────────────────────────────────────────────

/** Props for AlertDialogOverlay (internal use — not exported). */
type AlertDialogOverlayProps = React.ComponentPropsWithoutRef<
  typeof RadixAlertDialog.Overlay
>;

/** Backdrop overlay (internal use — not exported). */
function AlertDialogOverlay({ className, ...props }: AlertDialogOverlayProps) {
  return (
    <RadixAlertDialog.Overlay
      data-motion-role="overlay"
      className={cn(
        'z-overlay bg-surface-overlay fixed inset-0',
        'motion-overlay-enter motion-overlay-exit',
        className,
      )}
      {...props}
    />
  );
}

// ─── Content ──────────────────────────────────────────────────────────────

/** Props for AlertDialogContent */
export type AlertDialogContentProps = React.ComponentPropsWithoutRef<
  typeof RadixAlertDialog.Content
>;

/** Alert dialog panel - for destructive confirmations. */
export function AlertDialogContent({ className, children, ...props }: AlertDialogContentProps) {
  return (
    <AlertDialogPortal>
      <AlertDialogOverlay />
      <RadixAlertDialog.Content
        data-motion-role="panel"
        className={cn(
          'z-modal fixed left-1/2 top-1/2',
          '-translate-x-1/2 -translate-y-1/2',
          'w-full max-w-sm',
          'rounded-xl border border-border-default bg-surface-base',
          'shadow-xl',
          'p-6',
          'focus:outline-none',
          'motion-overlay-panel-enter motion-overlay-panel-exit',
          className,
        )}
        {...props}
      >
        {children}
      </RadixAlertDialog.Content>
    </AlertDialogPortal>
  );
}

// ─── Sub-components ────────────────────────────────────────────────────────

/** Props for AlertDialogHeader */
export type AlertDialogHeaderProps = React.HTMLAttributes<HTMLDivElement>;

/** Header for alert dialog. */
export function AlertDialogHeader({ className, ...props }: AlertDialogHeaderProps) {
  return <div className={cn('mb-4 flex flex-col gap-1.5', className)} {...props} />;
}

/** Props for AlertDialogFooter */
export type AlertDialogFooterProps = React.HTMLAttributes<HTMLDivElement>;

/** Footer for alert dialog action buttons. */
export function AlertDialogFooter({ className, ...props }: AlertDialogFooterProps) {
  return <div className={cn('mt-6 flex flex-row-reverse gap-2', className)} {...props} />;
}

/** Props for AlertDialogTitle */
export type AlertDialogTitleProps = React.ComponentPropsWithoutRef<typeof RadixAlertDialog.Title>;

/** Alert dialog title - required. */
export function AlertDialogTitle({ className, ...props }: AlertDialogTitleProps) {
  return (
    <RadixAlertDialog.Title
      className={cn('text-lg font-bold text-neutral-900', className)}
      {...props}
    />
  );
}

/** Props for AlertDialogDescription */
export type AlertDialogDescriptionProps = React.ComponentPropsWithoutRef<
  typeof RadixAlertDialog.Description
>;

/** Alert dialog description. */
export function AlertDialogDescription({ className, ...props }: AlertDialogDescriptionProps) {
  return (
    <RadixAlertDialog.Description
      className={cn('text-sm text-text-muted', className)}
      {...props}
    />
  );
}

/** Props for AlertDialogAction */
export type AlertDialogActionProps = React.ComponentPropsWithoutRef<typeof RadixAlertDialog.Action>;

/** Confirm action button - typically destructive. */
export function AlertDialogAction({ className, ...props }: AlertDialogActionProps) {
  return (
    <RadixAlertDialog.Action
      className={cn(
        'inline-flex items-center justify-center',
        'bg-danger-600 rounded-md px-4 py-2',
        'text-sm font-medium text-white',
        'motion-press-feedback hover:bg-danger-700 transition-colors',
        'focus-visible:ring-danger-500 focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:outline-none',
        className,
      )}
      {...props}
    />
  );
}

/** Props for AlertDialogCancel */
export type AlertDialogCancelProps = React.ComponentPropsWithoutRef<typeof RadixAlertDialog.Cancel>;

/** Cancel button - closes without action. */
export function AlertDialogCancel({ className, ...props }: AlertDialogCancelProps) {
  return (
    <RadixAlertDialog.Cancel
      className={cn(
        'inline-flex items-center justify-center',
        'rounded-md border border-border-default bg-surface-base px-4 py-2',
        'text-sm font-medium text-neutral-700',
        'motion-press-feedback transition-colors hover:bg-neutral-50',
        'focus-visible:ring-2 focus-visible:ring-neutral-400 focus-visible:ring-offset-2 focus-visible:outline-none',
        className,
      )}
      {...props}
    />
  );
}
