import { forwardRef } from 'react';
import { View } from 'react-native';
import { Slot, useDialog } from '@cdx-ui/primitives';
import { composeEventHandlers } from '@cdx-ui/utils';
import { Button, type ButtonProps } from '../Button';
import { Dialog, type DialogRootProps, type DialogPopupProps } from '../Dialog';

// =============================================================================
// ALERT DIALOG ROOT (Dialog with non-dismissible defaults + alertdialog role)
// =============================================================================

export interface AlertDialogRootProps extends DialogRootProps {}

const AlertDialogRoot = forwardRef<View, AlertDialogRootProps>(
  (
    {
      closeOnBackdropPress = false,
      closeOnEscKey = false,
      role = 'alertdialog',
      children,
      ...props
    },
    ref,
  ) => (
    <Dialog
      ref={ref}
      closeOnBackdropPress={closeOnBackdropPress}
      closeOnEscKey={closeOnEscKey}
      role={role}
      {...props}
    >
      {children}
    </Dialog>
  ),
);
AlertDialogRoot.displayName = 'AlertDialog';

// =============================================================================
// ALERT DIALOG POPUP (swipe disabled by default)
// =============================================================================

export interface AlertDialogPopupProps extends DialogPopupProps {}

const AlertDialogPopup = forwardRef<View, AlertDialogPopupProps>(
  ({ isSwipeable = false, ...props }, ref) => (
    <Dialog.Popup ref={ref} isSwipeable={isSwipeable} {...props} />
  ),
);
AlertDialogPopup.displayName = 'AlertDialog.Popup';

// =============================================================================
// ALERT DIALOG ACTION
// =============================================================================

export interface AlertDialogActionProps extends Omit<ButtonProps, 'variant'> {
  readonly asChild?: boolean;
}

const AlertDialogAction = forwardRef<View, AlertDialogActionProps>(
  ({ asChild = false, children, color = 'action', className, ...props }, ref) => {
    if (asChild) {
      return (
        <Slot ref={ref} className={className} {...props}>
          {children}
        </Slot>
      );
    }

    return (
      <Button ref={ref} variant="solid" color={color} className={className} {...props}>
        <Button.Label>{children}</Button.Label>
      </Button>
    );
  },
);
AlertDialogAction.displayName = 'AlertDialog.Action';

// =============================================================================
// ALERT DIALOG CANCEL
// =============================================================================

export interface AlertDialogCancelProps extends Omit<ButtonProps, 'variant'> {
  readonly asChild?: boolean;
}

const AlertDialogCancel = forwardRef<View, AlertDialogCancelProps>(
  ({ asChild = false, children, onPress, className, ...props }, ref) => {
    const { onOpenChange } = useDialog();
    const handlePress = composeEventHandlers(onPress, () => onOpenChange(false));

    if (asChild) {
      return (
        <Slot ref={ref} onPress={handlePress} className={className} {...props}>
          {children}
        </Slot>
      );
    }

    return (
      <Button ref={ref} variant="ghost" onPress={handlePress} className={className} {...props}>
        <Button.Label>{children}</Button.Label>
      </Button>
    );
  },
);
AlertDialogCancel.displayName = 'AlertDialog.Cancel';

// =============================================================================
// COMPOUND EXPORT
// =============================================================================

type AlertDialogCompoundComponent = typeof AlertDialogRoot & {
  Trigger: typeof Dialog.Trigger;
  Popup: typeof AlertDialogPopup;
  Header: typeof Dialog.Header;
  Title: typeof Dialog.Title;
  Description: typeof Dialog.Description;
  Content: typeof Dialog.Content;
  Text: typeof Dialog.Text;
  Footer: typeof Dialog.Footer;
  Close: typeof Dialog.Close;
  Action: typeof AlertDialogAction;
  Cancel: typeof AlertDialogCancel;
};

export const AlertDialog = Object.assign(AlertDialogRoot, {
  Trigger: Dialog.Trigger,
  Popup: AlertDialogPopup,
  Header: Dialog.Header,
  Title: Dialog.Title,
  Description: Dialog.Description,
  Content: Dialog.Content,
  Text: Dialog.Text,
  Footer: Dialog.Footer,
  Close: Dialog.Close,
  Action: AlertDialogAction,
  Cancel: AlertDialogCancel,
}) as AlertDialogCompoundComponent;
