import React, { useState } from 'react';
import { View, StyleSheet, ScrollView, Alert } from 'react-native';
import { Ionicons } from '@expo/vector-icons';
import { colors, spacing, radii } from '../../theme/tokens';
import { SafeScreen } from '../../components/layout/SafeScreen';
import { Header } from '../../components/layout/Header';
import { H3, Body, BodyMedium, Caption, Card, Button } from '../../components/ui';
import { t } from '../../i18n';
import { settingsRepo } from '../../db';

export function PrivacyScreen({ navigation }: any) {
  const [exporting, setExporting] = useState(false);

  const handleExport = async () => {
    setExporting(true);
    try {
      const data = await settingsRepo.exportAll();
      Alert.alert(
        t('privacy.exportData'),
        `${data.length} characters exported. In production, this would save a JSON file.`
      );
    } catch (e) {
      Alert.alert(t('common.error'));
    }
    setExporting(false);
  };

  const handleDeleteAll = () => {
    Alert.alert(
      t('privacy.deleteAll'),
      t('privacy.deleteConfirm'),
      [
        { text: t('common.cancel'), style: 'cancel' },
        {
          text: t('common.delete'),
          style: 'destructive',
          onPress: async () => {
            await settingsRepo.deleteAllData();
            Alert.alert('OK', 'All data deleted.');
          },
        },
      ]
    );
  };

  return (
    <SafeScreen backgroundColor={colors.background}>
      <Header title={t('privacy.title')} onBack={() => navigation.goBack()} />
      <ScrollView showsVerticalScrollIndicator={false} contentContainerStyle={styles.scrollContent}>
        {/* Local first info */}
        <View style={styles.infoBox}>
          <Ionicons name="shield-checkmark" size={32} color={colors.brand.green} />
          <View style={styles.infoContent}>
            <BodyMedium>{t('privacy.localFirst')}</BodyMedium>
            <Body style={styles.infoDesc}>{t('privacy.explanation')}</Body>
          </View>
        </View>

        <Card style={styles.featureCard}>
          <View style={styles.featureRow}>
            <Ionicons name="phone-portrait-outline" size={22} color={colors.secondary[500]} />
            <View style={styles.featureInfo}>
              <BodyMedium>{t('privacy.localFirst')}</BodyMedium>
              <Caption>{t('privacy.localFirstDesc')}</Caption>
            </View>
          </View>
        </Card>

        <Card style={styles.featureCard}>
          <View style={styles.featureRow}>
            <Ionicons name="cloud-offline-outline" size={22} color={colors.primary[500]} />
            <View style={styles.featureInfo}>
              <BodyMedium>{t('privacy.noCloud')}</BodyMedium>
              <Caption>{t('privacy.noCloudDesc')}</Caption>
            </View>
          </View>
        </Card>

        {/* Actions */}
        <H3 style={styles.sectionTitle}>{t('settings.account')}</H3>

        <Button
          title={t('privacy.exportData')}
          onPress={handleExport}
          variant="outline"
          fullWidth
          loading={exporting}
          icon={<Ionicons name="download-outline" size={18} color={colors.primary[500]} />}
        />

        <View style={styles.deleteSection}>
          <Button
            title={t('privacy.deleteAll')}
            onPress={handleDeleteAll}
            variant="ghost"
            fullWidth
            icon={<Ionicons name="trash-outline" size={18} color={colors.error} />}
          />
          <Caption style={styles.deleteHint}>{t('privacy.deleteAllDesc')}</Caption>
        </View>
      </ScrollView>
    </SafeScreen>
  );
}

const styles = StyleSheet.create({
  scrollContent: {
    paddingHorizontal: spacing.xl,
    paddingBottom: spacing['5xl'],
  },
  infoBox: {
    flexDirection: 'row',
    alignItems: 'flex-start',
    gap: spacing.lg,
    padding: spacing.xl,
    backgroundColor: colors.secondary[50],
    borderRadius: radii['2xl'],
    marginBottom: spacing.xl,
  },
  infoContent: { flex: 1 },
  infoDesc: { marginTop: spacing.xs, fontSize: 13, lineHeight: 20, color: colors.neutral[600] },
  featureCard: {
    padding: spacing.lg,
    marginBottom: spacing.sm,
  },
  featureRow: {
    flexDirection: 'row',
    alignItems: 'center',
    gap: spacing.md,
  },
  featureInfo: { flex: 1 },
  sectionTitle: {
    marginTop: spacing['2xl'],
    marginBottom: spacing.lg,
  },
  deleteSection: {
    marginTop: spacing.xl,
    alignItems: 'center',
  },
  deleteHint: {
    color: colors.neutral[400],
    marginTop: spacing.xs,
  },
});
