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

export function CommonPotScreen() {
  // MVP mock data
  const pot = {
    target: 3000,
    contributionA: 1800,
    contributionB: 900,
    balance: 2700,
  };
  const progress = pot.balance / pot.target;

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

        {/* Main pot card */}
        <Animated.View entering={FadeInDown.delay(100).duration(500)}>
          <LinearGradient
            colors={[colors.secondary[600], colors.brand.green]}
            style={styles.potCard}
            start={{ x: 0, y: 0 }}
            end={{ x: 1, y: 1 }}
          >
            <Caption style={styles.potLabel}>{t('pot.balance')}</Caption>
            <H2 style={styles.potAmount}>{formatCurrency(pot.balance)}</H2>
            <View style={styles.potProgress}>
              <ProgressBar progress={progress} color="#FFFFFF" trackColor="rgba(255,255,255,0.25)" height={6} />
            </View>
            <View style={styles.potFooterRow}>
              <Caption style={styles.potFooterText}>
                {t('pot.targetMonthly')}: {formatCurrency(pot.target)}
              </Caption>
              <Badge
                label={progress >= 0.9 ? t('pot.onTrack') : t('pot.behind')}
                variant={progress >= 0.9 ? 'success' : 'warning'}
              />
            </View>
          </LinearGradient>
        </Animated.View>

        {/* Contributions */}
        <H3 style={styles.sectionTitle}>{t('pot.contributions')}</H3>
        <Animated.View entering={FadeInDown.delay(200).duration(400)}>
          <Card style={styles.contribCard}>
            <View style={styles.contribRow}>
              <View style={styles.contribLeft}>
                <View style={[styles.contribDot, { backgroundColor: colors.primary[500] }]} />
                <View>
                  <BodyMedium>{t('quiz.partnerA')}</BodyMedium>
                  <Caption>{formatCurrency(pot.contributionA)} {t('pot.contributed')}</Caption>
                </View>
              </View>
              <BodyMedium style={styles.contribPercent}>
                {Math.round((pot.contributionA / pot.target) * 100)}%
              </BodyMedium>
            </View>
            <ProgressBar
              progress={pot.contributionA / pot.target}
              color={colors.primary[500]}
              height={4}
            />
          </Card>
        </Animated.View>

        <Animated.View entering={FadeInDown.delay(300).duration(400)}>
          <Card style={styles.contribCard}>
            <View style={styles.contribRow}>
              <View style={styles.contribLeft}>
                <View style={[styles.contribDot, { backgroundColor: colors.secondary[500] }]} />
                <View>
                  <BodyMedium>{t('quiz.partnerB')}</BodyMedium>
                  <Caption>{formatCurrency(pot.contributionB)} {t('pot.contributed')}</Caption>
                </View>
              </View>
              <BodyMedium style={styles.contribPercent}>
                {Math.round((pot.contributionB / pot.target) * 100)}%
              </BodyMedium>
            </View>
            <ProgressBar
              progress={pot.contributionB / pot.target}
              color={colors.secondary[500]}
              height={4}
            />
          </Card>
        </Animated.View>

        {/* Remaining */}
        <Animated.View entering={FadeInDown.delay(400).duration(400)}>
          <Card style={styles.remainingCard} variant="outlined">
            <View style={styles.remainingRow}>
              <Ionicons name="trending-up-outline" size={24} color={colors.neutral[500]} />
              <View style={styles.remainingInfo}>
                <Caption>{t('pot.remaining')}</Caption>
                <BodyMedium style={styles.remainingAmount}>
                  {formatCurrency(pot.target - pot.balance)}
                </BodyMedium>
              </View>
            </View>
          </Card>
        </Animated.View>
      </ScrollView>
    </SafeScreen>
  );
}

const styles = StyleSheet.create({
  scrollContent: {
    paddingHorizontal: spacing.xl,
    paddingBottom: spacing['5xl'],
  },
  title: {
    paddingTop: spacing.lg,
    marginBottom: spacing.xl,
  },
  potCard: {
    borderRadius: radii['3xl'],
    padding: spacing['2xl'],
    marginBottom: spacing.xl,
    ...shadows.lg,
  },
  potLabel: {
    color: 'rgba(255,255,255,0.7)',
    textTransform: 'uppercase',
    letterSpacing: 1.5,
  },
  potAmount: {
    color: '#FFFFFF',
    fontSize: 36,
    marginVertical: spacing.md,
  },
  potProgress: {
    marginBottom: spacing.md,
  },
  potFooterRow: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'center',
  },
  potFooterText: { color: 'rgba(255,255,255,0.7)' },
  sectionTitle: {
    marginBottom: spacing.md,
  },
  contribCard: {
    padding: spacing.xl,
    marginBottom: spacing.md,
  },
  contribRow: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'center',
    marginBottom: spacing.md,
  },
  contribLeft: {
    flexDirection: 'row',
    alignItems: 'center',
    gap: spacing.md,
  },
  contribDot: {
    width: 12,
    height: 12,
    borderRadius: 6,
  },
  contribPercent: {
    color: colors.neutral[600],
    fontSize: 18,
  },
  remainingCard: {
    padding: spacing.xl,
    marginTop: spacing.md,
  },
  remainingRow: {
    flexDirection: 'row',
    alignItems: 'center',
    gap: spacing.lg,
  },
  remainingInfo: { flex: 1 },
  remainingAmount: {
    fontSize: 18,
    marginTop: spacing.xs,
  },
});
