/**
 * ModuleErrorFallback - 模块加载失败的兜底组件
 */

import React from 'react';
import { View, Text, TouchableOpacity, StyleSheet } from 'react-native';

interface Props {
  moduleId: string;
  error: Error | null;
  onRetry: () => void;
  onGoHome?: () => void;
}

export function ModuleErrorFallback({
  moduleId,
  error,
  onRetry,
  onGoHome,
}: Props) {
  return (
    <View style={styles.container}>
      <Text style={styles.title}>{moduleId} 模块暂时无法使用</Text>
      {error && (
        <Text style={styles.errorText} selectable>
          {error.message || String(error)}
        </Text>
      )}
      <View style={styles.buttonContainer}>
        <TouchableOpacity style={styles.button} onPress={onRetry}>
          <Text style={styles.buttonText}>重试</Text>
        </TouchableOpacity>
        {onGoHome && (
          <TouchableOpacity style={styles.button} onPress={onGoHome}>
            <Text style={styles.buttonText}>返回首页</Text>
          </TouchableOpacity>
        )}
      </View>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#fff',
    padding: 20,
  },
  title: {
    fontSize: 20,
    fontWeight: 'bold',
    color: '#333',
    marginBottom: 16,
    textAlign: 'center',
  },
  errorText: {
    fontSize: 14,
    color: '#d32f2f',
    marginBottom: 24,
    textAlign: 'center',
    padding: 12,
    backgroundColor: '#ffebee',
    borderRadius: 4,
    width: '100%',
  },
  buttonContainer: {
    width: '100%',
    maxWidth: 300,
  },
  button: {
    backgroundColor: '#007AFF',
    padding: 15,
    borderRadius: 8,
    marginVertical: 8,
    alignItems: 'center',
  },
  buttonText: {
    color: '#fff',
    fontSize: 16,
    fontWeight: '600',
  },
});

