import React from 'react';
import { View, StyleSheet, ScrollView } from 'react-native';
import Animated, { FadeInDown } from 'react-native-reanimated';
import { Ionicons } from '@expo/vector-icons';
import { colors, spacing, radii } from '../../theme/tokens';
import { SafeScreen } from '../../components/layout/SafeScreen';
import { H2, H3, Body, BodyMedium, Caption, Card, ProgressBar, Badge, Button, EmptyState } from '../../components/ui';
import { t } from '../../i18n';
import { formatCurrency } from '../../utils/format';

const MOCK_BUDGETS = [
  { category: 'housing', limit: 1500, spent: 1500, icon: 'home-outline' as const },
  { category: 'groceries', limit: 600, spent: 420, icon: 'cart-outline' as const },
  { category: 'transport', limit: 300, spent: 180, icon: 'car-outline' as const },
  { category: 'childcare', limit: 400, spent: 400, icon: 'people-outline' as const },
  { category: 'leisure', limit: 200, spent: 260, icon: 'game-controller-outline' as const },
  { category: 'utilities', limit: 250, spent: 230, icon: 'flash-outline' as const },
];

export function BudgetScreen() {
  const totalLimit = MOCK_BUDGETS.reduce((s, b) => s + b.limit, 0);
  const totalSpent = MOCK_BUDGETS.reduce((s, b) => s + b.spent, 0);

  return (
    <SafeScreen scroll padded={false}>
      <ScrollView showsVerticalScrollIndicator={false} contentContainerStyle={styles.scrollContent}>
        <View style={styles.header}>
          <H2>{t('budget.title')}</H2>
        </View>

        {/* Summary */}
        <Animated.View entering={FadeInDown.delay(100).duration(400)}>
          <Card style={styles.summaryCard} variant="elevated">
            <View style={styles.summaryRow}>
              <View>
                <Caption>{t('budget.spent')}</Caption>
                <H3 style={{ color: totalSpent > totalLimit ? colors.error : colors.neutral[900] }}>
                  {formatCurrency(totalSpent)}
                </H3>
              </View>
              <View style={{ alignItems: 'flex-end' }}>
                <Caption>{t('budget.limits')}</Caption>
                <H3>{formatCurrency(totalLimit)}</H3>
              </View>
            </View>
            <ProgressBar
              progress={totalSpent / totalLimit}
              color={totalSpent > totalLimit ? colors.error : colors.secondary[500]}
              height={8}
            />
          </Card>
        </Animated.View>

        {/* Categories */}
        <H3 style={styles.sectionTitle}>{t('budget.categories')}</H3>
        {MOCK_BUDGETS.map((budget, index) => {
          const progress = budget.spent / budget.limit;
          const isOver = progress > 1;
          return (
            <Animated.View
              key={budget.category}
              entering={FadeInDown.delay(150 + index * 80).duration(400)}
            >
              <Card style={styles.budgetCard}>
                <View style={styles.budgetHeader}>
                  <View style={styles.budgetIcon}>
                    <Ionicons
                      name={budget.icon}
                      size={20}
                      color={isOver ? colors.error : colors.primary[500]}
                    />
                  </View>
                  <View style={styles.budgetInfo}>
                    <BodyMedium>{t(`quiz.${budget.category}`)}</BodyMedium>
                    <Caption>
                      {formatCurrency(budget.spent)} {t('budget.of')} {formatCurrency(budget.limit)}
                    </Caption>
                  </View>
                  {isOver && <Badge label={t('budget.overBudget')} variant="error" />}
                </View>
                <ProgressBar
                  progress={Math.min(progress, 1)}
                  color={isOver ? colors.error : colors.secondary[500]}
                  height={4}
                />
              </Card>
            </Animated.View>
          );
        })}
      </ScrollView>
    </SafeScreen>
  );
}

const styles = StyleSheet.create({
  scrollContent: {
    paddingHorizontal: spacing.xl,
    paddingBottom: spacing['5xl'],
  },
  header: {
    paddingTop: spacing.lg,
    marginBottom: spacing.xl,
  },
  summaryCard: {
    padding: spacing.xl,
    marginBottom: spacing.xl,
  },
  summaryRow: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    marginBottom: spacing.lg,
  },
  sectionTitle: {
    marginBottom: spacing.md,
  },
  budgetCard: {
    padding: spacing.lg,
    marginBottom: spacing.md,
  },
  budgetHeader: {
    flexDirection: 'row',
    alignItems: 'center',
    marginBottom: spacing.md,
  },
  budgetIcon: {
    width: 40,
    height: 40,
    borderRadius: 20,
    backgroundColor: colors.neutral[50],
    alignItems: 'center',
    justifyContent: 'center',
    marginRight: spacing.md,
  },
  budgetInfo: {
    flex: 1,
  },
});
