import React, { useState, useEffect, useCallback, useMemo } from 'react';
import {
  View,
  Text,
  StyleSheet,
  ScrollView,
  TouchableOpacity,
  RefreshControl,
  Image,
  Modal,
  TextInput,
  Alert,
  KeyboardAvoidingView,
  Platform,
  Animated,
} from 'react-native';
import { useLocalSearchParams, router } from 'expo-router';
import { Ionicons } from '@expo/vector-icons';
import { SafeAreaView } from 'react-native-safe-area-context';
import * as ImagePicker from 'expo-image-picker';
import { api } from '@/services/api';
import { useBeneficiary } from '@/contexts/BeneficiaryContext';
import { LoadingSpinner } from '@/components/ui/LoadingSpinner';
import { FullScreenError } from '@/components/ui/ErrorMessage';
import { Button } from '@/components/ui/Button';
import {
  AppColors,
  BorderRadius,
  FontSizes,
  Spacing,
  FontWeights,
  Shadows,
  AvatarSizes,
} from '@/constants/theme';
import type { Beneficiary } from '@/types';

// Local beneficiaries have timestamp-based IDs (>1000000000)
const isLocalBeneficiary = (id: string | number): boolean => {
  const numId = typeof id === 'string' ? parseInt(id, 10) : id;
  return numId > 1000000000;
};

// Setup state types
type SetupState = 'loading' | 'no_devices' | 'no_subscription' | 'ready';

// No Devices Screen Component
function NoDevicesScreen({
  beneficiary,
  onActivate,
  onGetSensors
}: {
  beneficiary: Beneficiary;
  onActivate: () => void;
  onGetSensors: () => void;
}) {
  return (
    <View style={styles.setupContainer}>
      <View style={styles.setupIconContainer}>
        <Ionicons name="hardware-chip-outline" size={48} color={AppColors.primary} />
      </View>
      <Text style={styles.setupTitle}>Connect Sensors</Text>
      <Text style={styles.setupSubtitle}>
        To start monitoring {beneficiary.name}'s wellness, you need to connect sensors first.
      </Text>

      <View style={styles.setupOptions}>
        <TouchableOpacity style={styles.setupOptionCard} onPress={onActivate}>
          <View style={[styles.setupOptionIcon, { backgroundColor: AppColors.primaryLighter }]}>
            <Ionicons name="qr-code-outline" size={28} color={AppColors.primary} />
          </View>
          <Text style={styles.setupOptionTitle}>I have sensors</Text>
          <Text style={styles.setupOptionText}>
            Enter activation code to connect your WellNuo sensors
          </Text>
          <View style={styles.setupOptionArrow}>
            <Ionicons name="arrow-forward" size={20} color={AppColors.primary} />
          </View>
        </TouchableOpacity>

        <TouchableOpacity style={styles.setupOptionCard} onPress={onGetSensors}>
          <View style={[styles.setupOptionIcon, { backgroundColor: AppColors.accentLight }]}>
            <Ionicons name="cart-outline" size={28} color={AppColors.accent} />
          </View>
          <Text style={styles.setupOptionTitle}>Get sensors</Text>
          <Text style={styles.setupOptionText}>
            Order WellNuo sensor kit for comprehensive monitoring
          </Text>
          <View style={styles.setupOptionArrow}>
            <Ionicons name="arrow-forward" size={20} color={AppColors.accent} />
          </View>
        </TouchableOpacity>
      </View>
    </View>
  );
}

// No Subscription Screen Component
function NoSubscriptionScreen({
  beneficiary,
  onSubscribe
}: {
  beneficiary: Beneficiary;
  onSubscribe: () => void;
}) {
  const isExpired = beneficiary.subscription?.status === 'expired';

  return (
    <View style={styles.setupContainer}>
      <View style={[styles.setupIconContainer, isExpired && { backgroundColor: AppColors.errorLight }]}>
        <Ionicons
          name={isExpired ? "time-outline" : "diamond-outline"}
          size={48}
          color={isExpired ? AppColors.error : AppColors.accent}
        />
      </View>
      <Text style={styles.setupTitle}>
        {isExpired ? 'Subscription Expired' : 'Subscription Required'}
      </Text>
      <Text style={styles.setupSubtitle}>
        {isExpired
          ? `Your subscription for ${beneficiary.name} has expired. Renew to continue monitoring.`
          : `Activate a subscription to start monitoring ${beneficiary.name}'s wellness data.`
        }
      </Text>

      <View style={styles.subscriptionPriceCard}>
        <View style={styles.subscriptionPriceHeader}>
          <Text style={styles.subscriptionPriceLabel}>WellNuo Pro</Text>
          <View style={styles.subscriptionPriceBadge}>
            <Text style={styles.subscriptionPriceAmount}>$49</Text>
            <Text style={styles.subscriptionPriceUnit}>/month</Text>
          </View>
        </View>

        <View style={styles.subscriptionPriceFeatures}>
          <View style={styles.featureRow}>
            <Ionicons name="checkmark-circle" size={18} color={AppColors.success} />
            <Text style={styles.featureRowText}>24/7 AI monitoring</Text>
          </View>
          <View style={styles.featureRow}>
            <Ionicons name="checkmark-circle" size={18} color={AppColors.success} />
            <Text style={styles.featureRowText}>Unlimited chat with Julia AI</Text>
          </View>
          <View style={styles.featureRow}>
            <Ionicons name="checkmark-circle" size={18} color={AppColors.success} />
            <Text style={styles.featureRowText}>Detailed activity reports</Text>
          </View>
          <View style={styles.featureRow}>
            <Ionicons name="checkmark-circle" size={18} color={AppColors.success} />
            <Text style={styles.featureRowText}>Smart alerts & notifications</Text>
          </View>
        </View>
      </View>

      <Button
        title={isExpired ? "Renew Subscription" : "Subscribe Now"}
        onPress={onSubscribe}
        fullWidth
        size="lg"
      />
    </View>
  );
}

export default function BeneficiaryDetailScreen() {
  const { id } = useLocalSearchParams<{ id: string }>();
  const { setCurrentBeneficiary, localBeneficiaries, updateLocalBeneficiary, removeLocalBeneficiary } = useBeneficiary();
  const [beneficiary, setBeneficiary] = useState<Beneficiary | null>(null);
  const [isLoading, setIsLoading] = useState(true);
  const [isRefreshing, setIsRefreshing] = useState(false);
  const [error, setError] = useState<string | null>(null);

  // Check if this is a local beneficiary
  const isLocal = useMemo(() => id ? isLocalBeneficiary(id) : false, [id]);

  // Determine setup state
  const setupState = useMemo((): SetupState => {
    if (isLoading) return 'loading';
    if (!beneficiary) return 'loading';

    // Check if has devices
    const hasDevices = beneficiary.hasDevices ||
                       (beneficiary.devices && beneficiary.devices.length > 0) ||
                       beneficiary.device_id;

    if (!hasDevices) return 'no_devices';

    // Check subscription
    const subscription = beneficiary.subscription;
    if (!subscription || subscription.status === 'none' || subscription.status === 'expired') {
      return 'no_subscription';
    }

    return 'ready';
  }, [beneficiary, isLoading]);

  // Edit modal state
  const [isEditModalVisible, setIsEditModalVisible] = useState(false);
  const [editForm, setEditForm] = useState({
    name: '',
    address: '',
    avatar: '' as string | undefined,
  });

  // Modal animation
  const fadeAnim = React.useRef(new Animated.Value(0)).current;

  useEffect(() => {
    Animated.timing(fadeAnim, {
      toValue: isEditModalVisible ? 1 : 0,
      duration: 250,
      useNativeDriver: true,
    }).start();
  }, [isEditModalVisible]);

  const loadBeneficiary = useCallback(async (showLoading = true) => {
    if (!id) return;

    if (showLoading) setIsLoading(true);
    setError(null);

    try {
      // For local beneficiaries, get from context
      if (isLocal) {
        const localBeneficiary = localBeneficiaries.find(
          (b) => b.id === parseInt(id, 10)
        );
        if (localBeneficiary) {
          setBeneficiary(localBeneficiary);
        } else {
          setError('Beneficiary not found');
        }
      } else {
        // For real beneficiaries, fetch from API
        const response = await api.getBeneficiary(parseInt(id, 10));

        if (response.ok && response.data) {
          setBeneficiary(response.data);
        } else {
          setError(response.error?.message || 'Failed to load beneficiary');
        }
      }
    } catch (err) {
      setError(err instanceof Error ? err.message : 'An error occurred');
    } finally {
      setIsLoading(false);
      setIsRefreshing(false);
    }
  }, [id, isLocal, localBeneficiaries]);

  useEffect(() => {
    loadBeneficiary();
  }, [loadBeneficiary]);

  const handleRefresh = useCallback(() => {
    setIsRefreshing(true);
    loadBeneficiary(false);
  }, [loadBeneficiary]);

  const handleActivateSensors = () => {
    router.push({
      pathname: '/(tabs)/beneficiaries/[id]/activate',
      params: { id: id! },
    });
  };

  const handleGetSensors = () => {
    // For now, show info or redirect to purchase
    Alert.alert(
      'Get WellNuo Sensors',
      'WellNuo sensor kits include motion sensors, door sensors, and temperature monitors.\n\nVisit wellnuo.com to order.',
      [{ text: 'OK' }]
    );
  };

  const handleSubscribe = () => {
    router.push({
      pathname: '/(tabs)/beneficiaries/[id]/purchase',
      params: { id: id! },
    });
  };

  const handleEditPress = () => {
    if (beneficiary) {
      setEditForm({
        name: beneficiary.name || '',
        address: beneficiary.address || '',
        avatar: beneficiary.avatar,
      });
      setIsEditModalVisible(true);
    }
  };

  const handlePickAvatar = async () => {
    const { status } = await ImagePicker.requestMediaLibraryPermissionsAsync();
    if (status !== 'granted') {
      Alert.alert('Permission needed', 'Please allow access to your photo library.');
      return;
    }

    const result = await ImagePicker.launchImageLibraryAsync({
      mediaTypes: ['images'],
      allowsEditing: true,
      aspect: [1, 1],
      quality: 0.5,
    });

    if (!result.canceled && result.assets[0]) {
      setEditForm(prev => ({ ...prev, avatar: result.assets[0].uri }));
    }
  };

  const handleSaveEdit = async () => {
    if (!editForm.name.trim()) {
      Alert.alert('Error', 'Name is required');
      return;
    }

    if (isLocal && id) {
      const updated = await updateLocalBeneficiary(parseInt(id, 10), {
        name: editForm.name.trim(),
        address: editForm.address.trim() || undefined,
        avatar: editForm.avatar,
      });

      if (updated) {
        setBeneficiary(updated);
        setIsEditModalVisible(false);
      } else {
        Alert.alert('Error', 'Failed to save changes.');
      }
    } else {
      Alert.alert(
        'Demo Mode',
        'Saving beneficiary data requires backend API.',
        [{ text: 'OK', onPress: () => setIsEditModalVisible(false) }]
      );
    }
  };

  const showComingSoon = (featureName: string) => {
    Alert.alert(
      'Coming Soon',
      `${featureName} is currently in development.`,
      [{ text: 'OK' }]
    );
  };

  const handleDeleteBeneficiary = () => {
    if (!isLocal || !id) return;

    Alert.alert(
      'Remove Beneficiary',
      `Are you sure you want to remove ${beneficiary?.name}? This action cannot be undone.`,
      [
        { text: 'Cancel', style: 'cancel' },
        {
          text: 'Remove',
          style: 'destructive',
          onPress: async () => {
            try {
              await removeLocalBeneficiary(parseInt(id, 10));
              router.replace('/(tabs)');
            } catch (err) {
              Alert.alert('Error', 'Failed to remove beneficiary');
            }
          },
        },
      ]
    );
  };

  if (isLoading) {
    return <LoadingSpinner fullScreen message="Loading..." />;
  }

  if (error || !beneficiary) {
    return (
      <FullScreenError
        message={error || 'Beneficiary not found'}
        onRetry={() => loadBeneficiary()}
      />
    );
  }

  const statusColor = beneficiary.status === 'online' ? AppColors.online : AppColors.offline;

  // Render based on setup state
  const renderContent = () => {
    switch (setupState) {
      case 'no_devices':
        return (
          <NoDevicesScreen
            beneficiary={beneficiary}
            onActivate={handleActivateSensors}
            onGetSensors={handleGetSensors}
          />
        );

      case 'no_subscription':
        return (
          <NoSubscriptionScreen
            beneficiary={beneficiary}
            onSubscribe={handleSubscribe}
          />
        );

      case 'ready':
      default:
        return (
          <ScrollView
            style={styles.scrollView}
            contentContainerStyle={styles.scrollContent}
            showsVerticalScrollIndicator={false}
            refreshControl={
              <RefreshControl
                refreshing={isRefreshing}
                onRefresh={handleRefresh}
                tintColor={AppColors.primary}
              />
            }
          >
            {/* Profile Card */}
            <View style={styles.profileCard}>
              <View style={styles.avatarWrapper}>
                {beneficiary.avatar ? (
                  <Image source={{ uri: beneficiary.avatar }} style={styles.avatarImage} />
                ) : (
                  <View style={styles.avatar}>
                    <Text style={styles.avatarText}>
                      {beneficiary.name.charAt(0).toUpperCase()}
                    </Text>
                  </View>
                )}
                <View style={[styles.statusDot, { backgroundColor: statusColor }]} />
              </View>

              <Text style={styles.beneficiaryName}>{beneficiary.name}</Text>

              {beneficiary.address && (
                <View style={styles.locationRow}>
                  <Ionicons name="location" size={14} color={AppColors.textMuted} />
                  <Text style={styles.locationText}>{beneficiary.address}</Text>
                </View>
              )}

              <View style={styles.statusBadge}>
                <View style={[styles.statusIndicator, { backgroundColor: statusColor }]} />
                <Text style={styles.statusText}>
                  {beneficiary.status === 'online' ? 'Online now' : 'Offline'}
                </Text>
              </View>

              {beneficiary.last_activity && (
                <Text style={styles.lastActivity}>
                  Last activity: {beneficiary.last_activity}
                </Text>
              )}
            </View>

            {/* Quick Actions */}
            <Text style={styles.sectionTitle}>Quick Actions</Text>
            <View style={styles.actionsRow}>
              <TouchableOpacity
                style={styles.actionButton}
                onPress={() => router.push(`/(tabs)/beneficiaries/${id}/dashboard`)}
              >
                <View style={[styles.actionButtonIcon, { backgroundColor: AppColors.primaryLighter }]}>
                  <Ionicons name="stats-chart" size={22} color={AppColors.primary} />
                </View>
                <Text style={styles.actionButtonText}>Dashboard</Text>
              </TouchableOpacity>

              <TouchableOpacity
                style={styles.actionButton}
                onPress={() => {
                  setCurrentBeneficiary(beneficiary);
                  router.push('/(tabs)/chat');
                }}
              >
                <View style={[styles.actionButtonIcon, { backgroundColor: AppColors.accentLight }]}>
                  <Ionicons name="chatbubbles" size={22} color={AppColors.accent} />
                </View>
                <Text style={styles.actionButtonText}>Chat</Text>
              </TouchableOpacity>
            </View>

            {/* Subscription Section */}
            <Text style={styles.sectionTitle}>Subscription</Text>
            <View style={styles.subscriptionCard}>
              <View style={styles.subscriptionHeader}>
                <View style={styles.subscriptionBadgeContainer}>
                  <View style={styles.subscriptionIconBg}>
                    <Ionicons name="diamond" size={18} color={AppColors.accent} />
                  </View>
                  <View>
                    <Text style={styles.subscriptionPlan}>WellNuo Pro</Text>
                    <Text style={styles.subscriptionStatus}>Active</Text>
                  </View>
                </View>
                <View style={styles.priceBadge}>
                  <Text style={styles.priceText}>$49</Text>
                  <Text style={styles.priceUnit}>/mo</Text>
                </View>
              </View>

              <View style={styles.subscriptionDivider} />

              <View style={styles.subscriptionFeatures}>
                <View style={styles.featureItem}>
                  <Ionicons name="checkmark-circle" size={18} color={AppColors.success} />
                  <Text style={styles.featureText}>24/7 AI monitoring</Text>
                </View>
                <View style={styles.featureItem}>
                  <Ionicons name="checkmark-circle" size={18} color={AppColors.success} />
                  <Text style={styles.featureText}>Unlimited chat with Julia</Text>
                </View>
                <View style={styles.featureItem}>
                  <Ionicons name="checkmark-circle" size={18} color={AppColors.success} />
                  <Text style={styles.featureText}>Activity reports</Text>
                </View>
              </View>

              <TouchableOpacity
                style={styles.manageButton}
                onPress={() => showComingSoon('Subscription Management')}
              >
                <Text style={styles.manageButtonText}>Manage Subscription</Text>
                <Ionicons name="chevron-forward" size={18} color={AppColors.primary} />
              </TouchableOpacity>
            </View>

            {/* Settings */}
            <Text style={styles.sectionTitle}>Settings</Text>
            <View style={styles.settingsCard}>
              <TouchableOpacity style={styles.settingsRow} onPress={() => showComingSoon('Notification Settings')}>
                <View style={styles.settingsRowLeft}>
                  <View style={[styles.settingsIcon, { backgroundColor: AppColors.primarySubtle }]}>
                    <Ionicons name="notifications-outline" size={20} color={AppColors.primary} />
                  </View>
                  <Text style={styles.settingsRowText}>Notifications</Text>
                </View>
                <Ionicons name="chevron-forward" size={18} color={AppColors.textMuted} />
              </TouchableOpacity>

              <View style={styles.settingsDivider} />

              <TouchableOpacity style={styles.settingsRow} onPress={() => showComingSoon('Alert Rules')}>
                <View style={styles.settingsRowLeft}>
                  <View style={[styles.settingsIcon, { backgroundColor: AppColors.warningLight }]}>
                    <Ionicons name="alert-circle-outline" size={20} color={AppColors.warning} />
                  </View>
                  <Text style={styles.settingsRowText}>Alert Rules</Text>
                </View>
                <Ionicons name="chevron-forward" size={18} color={AppColors.textMuted} />
              </TouchableOpacity>

              <View style={styles.settingsDivider} />

              <TouchableOpacity style={styles.settingsRow} onPress={() => showComingSoon('Connected Sensors')}>
                <View style={styles.settingsRowLeft}>
                  <View style={[styles.settingsIcon, { backgroundColor: AppColors.infoLight }]}>
                    <Ionicons name="hardware-chip-outline" size={20} color={AppColors.info} />
                  </View>
                  <Text style={styles.settingsRowText}>Connected Sensors</Text>
                </View>
                <Ionicons name="chevron-forward" size={18} color={AppColors.textMuted} />
              </TouchableOpacity>
            </View>

            {/* Danger Zone - only for local beneficiaries */}
            {isLocal && (
              <>
                <Text style={[styles.sectionTitle, styles.dangerTitle]}>Danger Zone</Text>
                <TouchableOpacity style={styles.dangerButton} onPress={handleDeleteBeneficiary}>
                  <Ionicons name="trash-outline" size={20} color={AppColors.error} />
                  <Text style={styles.dangerButtonText}>Remove Beneficiary</Text>
                </TouchableOpacity>
              </>
            )}
          </ScrollView>
        );
    }
  };

  return (
    <SafeAreaView style={styles.container} edges={['top']}>
      {/* Header */}
      <View style={styles.header}>
        <TouchableOpacity style={styles.headerButton} onPress={() => router.back()}>
          <Ionicons name="arrow-back" size={22} color={AppColors.textPrimary} />
        </TouchableOpacity>
        <Text style={styles.headerTitle}>{beneficiary.name}</Text>
        <TouchableOpacity style={styles.headerButton} onPress={handleEditPress}>
          <Ionicons name="create-outline" size={22} color={AppColors.primary} />
        </TouchableOpacity>
      </View>

      {renderContent()}

      {/* Edit Modal */}
      <Modal
        visible={isEditModalVisible}
        animationType="slide"
        transparent={true}
        onRequestClose={() => setIsEditModalVisible(false)}
      >
        <KeyboardAvoidingView
          behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
          style={styles.modalOverlay}
        >
          <Animated.View style={[styles.modalBackdrop, { opacity: fadeAnim }]}>
            <TouchableOpacity
              style={StyleSheet.absoluteFill}
              activeOpacity={1}
              onPress={() => setIsEditModalVisible(false)}
            />
          </Animated.View>

          <View style={styles.modalContent}>
            {/* Modal Header */}
            <View style={styles.modalHeader}>
              <View style={styles.modalHeaderContent}>
                <View style={styles.modalIconContainer}>
                  <Ionicons name="person" size={22} color={AppColors.primary} />
                </View>
                <Text style={styles.modalTitle}>Edit Profile</Text>
              </View>
              <TouchableOpacity style={styles.modalCloseButton} onPress={() => setIsEditModalVisible(false)}>
                <Ionicons name="close" size={22} color={AppColors.textSecondary} />
              </TouchableOpacity>
            </View>

            <ScrollView style={styles.modalForm} showsVerticalScrollIndicator={false}>
              {/* Avatar Section */}
              <View style={styles.editAvatarSection}>
                <TouchableOpacity style={styles.editAvatarContainer} onPress={handlePickAvatar}>
                  {editForm.avatar ? (
                    <Image source={{ uri: editForm.avatar }} style={styles.editAvatarImage} />
                  ) : (
                    <Text style={styles.editAvatarText}>
                      {editForm.name ? editForm.name.charAt(0).toUpperCase() : '+'}
                    </Text>
                  )}
                  <View style={styles.editAvatarBadge}>
                    <Ionicons name="camera" size={14} color={AppColors.white} />
                  </View>
                </TouchableOpacity>
                <Text style={styles.editAvatarHint}>Tap to change photo</Text>
              </View>

              {/* Form Fields */}
              <View style={styles.formSection}>
                <Text style={styles.inputLabel}>Name</Text>
                <View style={styles.inputContainer}>
                  <Ionicons name="person-outline" size={20} color={AppColors.textMuted} />
                  <TextInput
                    style={styles.textInput}
                    value={editForm.name}
                    onChangeText={(text) => setEditForm({ ...editForm, name: text })}
                    placeholder="Enter name"
                    placeholderTextColor={AppColors.textMuted}
                  />
                </View>
              </View>

              <View style={styles.formSection}>
                <Text style={styles.inputLabel}>Address</Text>
                <View style={styles.inputContainer}>
                  <Ionicons name="location-outline" size={20} color={AppColors.textMuted} />
                  <TextInput
                    style={styles.textInput}
                    value={editForm.address}
                    onChangeText={(text) => setEditForm({ ...editForm, address: text })}
                    placeholder="Enter address"
                    placeholderTextColor={AppColors.textMuted}
                  />
                </View>
              </View>

              {/* Info Box */}
              <View style={styles.infoBox}>
                <Ionicons name="information-circle" size={20} color={AppColors.info} />
                <Text style={styles.infoBoxText}>
                  Wellness data is collected automatically from connected sensors.
                </Text>
              </View>
            </ScrollView>

            {/* Modal Footer */}
            <View style={styles.modalFooter}>
              <TouchableOpacity style={styles.cancelButton} onPress={() => setIsEditModalVisible(false)}>
                <Text style={styles.cancelButtonText}>Cancel</Text>
              </TouchableOpacity>
              <TouchableOpacity style={styles.saveButton} onPress={handleSaveEdit}>
                <Ionicons name="checkmark" size={20} color={AppColors.white} />
                <Text style={styles.saveButtonText}>Save</Text>
              </TouchableOpacity>
            </View>
          </View>
        </KeyboardAvoidingView>
      </Modal>
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: AppColors.background,
  },
  // Header
  header: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'space-between',
    paddingHorizontal: Spacing.lg,
    paddingVertical: Spacing.md,
    borderBottomWidth: 1,
    borderBottomColor: AppColors.border,
  },
  headerButton: {
    width: 40,
    height: 40,
    borderRadius: BorderRadius.md,
    backgroundColor: AppColors.surfaceSecondary,
    justifyContent: 'center',
    alignItems: 'center',
  },
  headerTitle: {
    fontSize: FontSizes.lg,
    fontWeight: FontWeights.semibold,
    color: AppColors.textPrimary,
  },
  scrollView: {
    flex: 1,
  },
  scrollContent: {
    padding: Spacing.lg,
    paddingBottom: Spacing.xxl,
  },
  // Setup Screens
  setupContainer: {
    flex: 1,
    padding: Spacing.xl,
    alignItems: 'center',
    justifyContent: 'center',
  },
  setupIconContainer: {
    width: 96,
    height: 96,
    borderRadius: 48,
    backgroundColor: AppColors.primaryLighter,
    justifyContent: 'center',
    alignItems: 'center',
    marginBottom: Spacing.lg,
  },
  setupTitle: {
    fontSize: FontSizes['2xl'],
    fontWeight: FontWeights.bold,
    color: AppColors.textPrimary,
    textAlign: 'center',
    marginBottom: Spacing.sm,
  },
  setupSubtitle: {
    fontSize: FontSizes.base,
    color: AppColors.textSecondary,
    textAlign: 'center',
    lineHeight: 24,
    marginBottom: Spacing.xl,
    paddingHorizontal: Spacing.md,
  },
  setupOptions: {
    width: '100%',
    gap: Spacing.md,
  },
  setupOptionCard: {
    backgroundColor: AppColors.surface,
    borderRadius: BorderRadius.xl,
    padding: Spacing.lg,
    ...Shadows.sm,
  },
  setupOptionIcon: {
    width: 56,
    height: 56,
    borderRadius: BorderRadius.lg,
    justifyContent: 'center',
    alignItems: 'center',
    marginBottom: Spacing.md,
  },
  setupOptionTitle: {
    fontSize: FontSizes.lg,
    fontWeight: FontWeights.semibold,
    color: AppColors.textPrimary,
    marginBottom: Spacing.xs,
  },
  setupOptionText: {
    fontSize: FontSizes.sm,
    color: AppColors.textSecondary,
    lineHeight: 20,
  },
  setupOptionArrow: {
    position: 'absolute',
    top: Spacing.lg,
    right: Spacing.lg,
    width: 32,
    height: 32,
    borderRadius: 16,
    backgroundColor: AppColors.surfaceSecondary,
    justifyContent: 'center',
    alignItems: 'center',
  },
  // Subscription Price Card
  subscriptionPriceCard: {
    width: '100%',
    backgroundColor: AppColors.surface,
    borderRadius: BorderRadius.xl,
    padding: Spacing.lg,
    marginBottom: Spacing.xl,
    ...Shadows.sm,
  },
  subscriptionPriceHeader: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'center',
    marginBottom: Spacing.md,
  },
  subscriptionPriceLabel: {
    fontSize: FontSizes.lg,
    fontWeight: FontWeights.semibold,
    color: AppColors.textPrimary,
  },
  subscriptionPriceBadge: {
    flexDirection: 'row',
    alignItems: 'baseline',
  },
  subscriptionPriceAmount: {
    fontSize: FontSizes['2xl'],
    fontWeight: FontWeights.bold,
    color: AppColors.primary,
  },
  subscriptionPriceUnit: {
    fontSize: FontSizes.sm,
    color: AppColors.textMuted,
    marginLeft: 2,
  },
  subscriptionPriceFeatures: {
    gap: Spacing.sm,
  },
  featureRow: {
    flexDirection: 'row',
    alignItems: 'center',
    gap: Spacing.sm,
  },
  featureRowText: {
    fontSize: FontSizes.sm,
    color: AppColors.textSecondary,
  },
  // Profile Card
  profileCard: {
    backgroundColor: AppColors.surface,
    borderRadius: BorderRadius.xl,
    padding: Spacing.xl,
    alignItems: 'center',
    marginBottom: Spacing.lg,
    ...Shadows.sm,
  },
  avatarWrapper: {
    position: 'relative',
    marginBottom: Spacing.md,
  },
  avatar: {
    width: AvatarSizes.xl,
    height: AvatarSizes.xl,
    borderRadius: AvatarSizes.xl / 2,
    backgroundColor: AppColors.primary,
    justifyContent: 'center',
    alignItems: 'center',
  },
  avatarImage: {
    width: AvatarSizes.xl,
    height: AvatarSizes.xl,
    borderRadius: AvatarSizes.xl / 2,
  },
  avatarText: {
    fontSize: FontSizes['3xl'],
    fontWeight: FontWeights.bold,
    color: AppColors.white,
  },
  statusDot: {
    position: 'absolute',
    bottom: 4,
    right: 4,
    width: 20,
    height: 20,
    borderRadius: 10,
    borderWidth: 3,
    borderColor: AppColors.surface,
  },
  beneficiaryName: {
    fontSize: FontSizes['2xl'],
    fontWeight: FontWeights.bold,
    color: AppColors.textPrimary,
    marginBottom: Spacing.xs,
  },
  locationRow: {
    flexDirection: 'row',
    alignItems: 'center',
    gap: Spacing.xs,
    marginBottom: Spacing.sm,
  },
  locationText: {
    fontSize: FontSizes.sm,
    color: AppColors.textMuted,
  },
  statusBadge: {
    flexDirection: 'row',
    alignItems: 'center',
    backgroundColor: AppColors.surfaceSecondary,
    paddingHorizontal: Spacing.md,
    paddingVertical: Spacing.xs,
    borderRadius: BorderRadius.full,
    gap: Spacing.xs,
  },
  statusIndicator: {
    width: 8,
    height: 8,
    borderRadius: 4,
  },
  statusText: {
    fontSize: FontSizes.sm,
    fontWeight: FontWeights.medium,
    color: AppColors.textSecondary,
  },
  lastActivity: {
    fontSize: FontSizes.xs,
    color: AppColors.textMuted,
    marginTop: Spacing.sm,
  },
  // Section
  sectionTitle: {
    fontSize: FontSizes.lg,
    fontWeight: FontWeights.semibold,
    color: AppColors.textPrimary,
    marginBottom: Spacing.md,
  },
  // Quick Actions Row
  actionsRow: {
    flexDirection: 'row',
    gap: Spacing.md,
    marginBottom: Spacing.lg,
  },
  actionButton: {
    flex: 1,
    backgroundColor: AppColors.surface,
    borderRadius: BorderRadius.xl,
    padding: Spacing.lg,
    alignItems: 'center',
    ...Shadows.xs,
  },
  actionButtonIcon: {
    width: 48,
    height: 48,
    borderRadius: BorderRadius.lg,
    justifyContent: 'center',
    alignItems: 'center',
    marginBottom: Spacing.sm,
  },
  actionButtonText: {
    fontSize: FontSizes.sm,
    fontWeight: FontWeights.medium,
    color: AppColors.textPrimary,
  },
  // Subscription Card
  subscriptionCard: {
    backgroundColor: AppColors.surface,
    borderRadius: BorderRadius.xl,
    padding: Spacing.lg,
    marginBottom: Spacing.lg,
    ...Shadows.sm,
  },
  subscriptionHeader: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'center',
  },
  subscriptionBadgeContainer: {
    flexDirection: 'row',
    alignItems: 'center',
    gap: Spacing.md,
  },
  subscriptionIconBg: {
    width: 44,
    height: 44,
    borderRadius: BorderRadius.lg,
    backgroundColor: AppColors.accentLight,
    justifyContent: 'center',
    alignItems: 'center',
  },
  subscriptionPlan: {
    fontSize: FontSizes.base,
    fontWeight: FontWeights.semibold,
    color: AppColors.textPrimary,
  },
  subscriptionStatus: {
    fontSize: FontSizes.sm,
    color: AppColors.success,
    fontWeight: FontWeights.medium,
  },
  priceBadge: {
    flexDirection: 'row',
    alignItems: 'baseline',
  },
  priceText: {
    fontSize: FontSizes.xl,
    fontWeight: FontWeights.bold,
    color: AppColors.textPrimary,
  },
  priceUnit: {
    fontSize: FontSizes.sm,
    color: AppColors.textMuted,
    marginLeft: 2,
  },
  subscriptionDivider: {
    height: 1,
    backgroundColor: AppColors.borderLight,
    marginVertical: Spacing.md,
  },
  subscriptionFeatures: {
    gap: Spacing.sm,
    marginBottom: Spacing.md,
  },
  featureItem: {
    flexDirection: 'row',
    alignItems: 'center',
    gap: Spacing.sm,
  },
  featureText: {
    fontSize: FontSizes.sm,
    color: AppColors.textSecondary,
  },
  manageButton: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'center',
    paddingVertical: Spacing.md,
    backgroundColor: AppColors.primarySubtle,
    borderRadius: BorderRadius.lg,
    gap: Spacing.xs,
  },
  manageButtonText: {
    fontSize: FontSizes.base,
    fontWeight: FontWeights.medium,
    color: AppColors.primary,
  },
  // Settings Card
  settingsCard: {
    backgroundColor: AppColors.surface,
    borderRadius: BorderRadius.xl,
    marginBottom: Spacing.lg,
    ...Shadows.xs,
  },
  settingsRow: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'space-between',
    padding: Spacing.md,
  },
  settingsRowLeft: {
    flexDirection: 'row',
    alignItems: 'center',
    gap: Spacing.md,
  },
  settingsIcon: {
    width: 36,
    height: 36,
    borderRadius: BorderRadius.md,
    justifyContent: 'center',
    alignItems: 'center',
  },
  settingsRowText: {
    fontSize: FontSizes.base,
    fontWeight: FontWeights.medium,
    color: AppColors.textPrimary,
  },
  settingsDivider: {
    height: 1,
    backgroundColor: AppColors.borderLight,
    marginLeft: 60,
  },
  // Danger Zone
  dangerTitle: {
    color: AppColors.error,
  },
  dangerButton: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: AppColors.errorLight,
    paddingVertical: Spacing.md,
    borderRadius: BorderRadius.lg,
    gap: Spacing.sm,
    marginBottom: Spacing.lg,
  },
  dangerButtonText: {
    fontSize: FontSizes.base,
    fontWeight: FontWeights.semibold,
    color: AppColors.error,
  },
  // Modal
  modalOverlay: {
    flex: 1,
    justifyContent: 'flex-end',
  },
  modalBackdrop: {
    ...StyleSheet.absoluteFillObject,
    backgroundColor: AppColors.overlay,
  },
  modalContent: {
    backgroundColor: AppColors.background,
    borderTopLeftRadius: BorderRadius['2xl'],
    borderTopRightRadius: BorderRadius['2xl'],
    maxHeight: '85%',
    ...Shadows.xl,
  },
  modalHeader: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'space-between',
    paddingHorizontal: Spacing.lg,
    paddingVertical: Spacing.lg,
    borderBottomWidth: 1,
    borderBottomColor: AppColors.border,
  },
  modalHeaderContent: {
    flexDirection: 'row',
    alignItems: 'center',
    gap: Spacing.md,
  },
  modalIconContainer: {
    width: 40,
    height: 40,
    borderRadius: BorderRadius.lg,
    backgroundColor: AppColors.primaryLighter,
    justifyContent: 'center',
    alignItems: 'center',
  },
  modalTitle: {
    fontSize: FontSizes.xl,
    fontWeight: FontWeights.bold,
    color: AppColors.textPrimary,
  },
  modalCloseButton: {
    width: 36,
    height: 36,
    borderRadius: BorderRadius.md,
    backgroundColor: AppColors.surfaceSecondary,
    justifyContent: 'center',
    alignItems: 'center',
  },
  modalForm: {
    padding: Spacing.lg,
  },
  // Edit Avatar
  editAvatarSection: {
    alignItems: 'center',
    marginBottom: Spacing.lg,
  },
  editAvatarContainer: {
    width: AvatarSizes.lg,
    height: AvatarSizes.lg,
    borderRadius: AvatarSizes.lg / 2,
    backgroundColor: AppColors.primary,
    justifyContent: 'center',
    alignItems: 'center',
    position: 'relative',
  },
  editAvatarImage: {
    width: AvatarSizes.lg,
    height: AvatarSizes.lg,
    borderRadius: AvatarSizes.lg / 2,
  },
  editAvatarText: {
    fontSize: FontSizes['2xl'],
    fontWeight: FontWeights.bold,
    color: AppColors.white,
  },
  editAvatarBadge: {
    position: 'absolute',
    bottom: 0,
    right: 0,
    width: 28,
    height: 28,
    borderRadius: 14,
    backgroundColor: AppColors.primary,
    justifyContent: 'center',
    alignItems: 'center',
    borderWidth: 3,
    borderColor: AppColors.background,
  },
  editAvatarHint: {
    fontSize: FontSizes.sm,
    color: AppColors.textMuted,
    marginTop: Spacing.sm,
  },
  // Form
  formSection: {
    marginBottom: Spacing.md,
  },
  inputLabel: {
    fontSize: FontSizes.sm,
    fontWeight: FontWeights.medium,
    color: AppColors.textSecondary,
    marginBottom: Spacing.xs,
  },
  inputContainer: {
    flexDirection: 'row',
    alignItems: 'center',
    backgroundColor: AppColors.surfaceSecondary,
    borderRadius: BorderRadius.lg,
    paddingHorizontal: Spacing.md,
    borderWidth: 1.5,
    borderColor: AppColors.borderLight,
    gap: Spacing.sm,
  },
  textInput: {
    flex: 1,
    fontSize: FontSizes.base,
    color: AppColors.textPrimary,
    paddingVertical: Spacing.md,
  },
  infoBox: {
    flexDirection: 'row',
    backgroundColor: AppColors.infoLight,
    borderRadius: BorderRadius.lg,
    padding: Spacing.md,
    gap: Spacing.sm,
    marginTop: Spacing.md,
  },
  infoBoxText: {
    flex: 1,
    fontSize: FontSizes.sm,
    color: AppColors.info,
    lineHeight: 20,
  },
  modalFooter: {
    flexDirection: 'row',
    padding: Spacing.lg,
    gap: Spacing.md,
    borderTopWidth: 1,
    borderTopColor: AppColors.border,
  },
  cancelButton: {
    flex: 1,
    paddingVertical: Spacing.md,
    borderRadius: BorderRadius.lg,
    backgroundColor: AppColors.surfaceSecondary,
    alignItems: 'center',
    justifyContent: 'center',
  },
  cancelButtonText: {
    fontSize: FontSizes.base,
    fontWeight: FontWeights.medium,
    color: AppColors.textSecondary,
  },
  saveButton: {
    flex: 2,
    flexDirection: 'row',
    paddingVertical: Spacing.md,
    borderRadius: BorderRadius.lg,
    backgroundColor: AppColors.primary,
    alignItems: 'center',
    justifyContent: 'center',
    gap: Spacing.xs,
    ...Shadows.primary,
  },
  saveButtonText: {
    fontSize: FontSizes.base,
    fontWeight: FontWeights.semibold,
    color: AppColors.white,
  },
});
