import React from 'react';
import { View, StyleSheet } from 'react-native';
import Animated, {
  useAnimatedStyle,
  withSpring,
} from 'react-native-reanimated';
import { colors, radii } from '../../theme/tokens';

interface ProgressBarProps {
  progress: number; // 0–1
  color?: string;
  trackColor?: string;
  height?: number;
}

export function ProgressBar({
  progress,
  color = colors.primary[500],
  trackColor = colors.neutral[100],
  height = 6,
}: ProgressBarProps) {
  const clampedProgress = Math.max(0, Math.min(1, progress));

  const fillStyle = useAnimatedStyle(() => ({
    width: withSpring(`${clampedProgress * 100}%` as any, {
      damping: 20,
      stiffness: 90,
    }),
  }));

  return (
    <View
      style={[styles.track, { backgroundColor: trackColor, height }]}
      accessibilityRole="progressbar"
      accessibilityValue={{
        min: 0,
        max: 100,
        now: Math.round(clampedProgress * 100),
      }}
    >
      <Animated.View
        style={[
          styles.fill,
          { backgroundColor: color, height },
          fillStyle,
        ]}
      />
    </View>
  );
}

const styles = StyleSheet.create({
  track: {
    width: '100%',
    borderRadius: radii.full,
    overflow: 'hidden',
  },
  fill: {
    borderRadius: radii.full,
  },
});
