import { Alert as RNAlert, Platform } from 'react-native';

interface AlertButton {
  text: string;
  onPress?: () => void;
  style?: 'default' | 'cancel' | 'destructive';
}

class PlatformAlert {
  alert(title: string, message?: string, buttons?: AlertButton[]) {
    if (Platform.OS === 'web') {
      this.webAlert(title, message, buttons);
    } else {
      RNAlert.alert(title, message, buttons);
    }
  }

  private webAlert(title: string, message?: string, buttons?: AlertButton[]) {
    const fullMessage = message ? `${title}\n\n${message}` : title;

    if (!buttons || buttons.length === 0) {
      window.alert(fullMessage);
      return;
    }

    if (buttons.length === 1) {
      window.alert(fullMessage);
      buttons[0].onPress?.();
      return;
    }

    const confirmed = window.confirm(fullMessage);

    if (confirmed) {
      const confirmButton = buttons.find(btn => btn.style !== 'cancel');
      confirmButton?.onPress?.();
    } else {
      const cancelButton = buttons.find(btn => btn.style === 'cancel');
      cancelButton?.onPress?.();
    }
  }
}

export const Alert = new PlatformAlert();
