import React from 'react';
import { View, Text, StyleSheet } from 'react-native';

type Step = {
  id: number;
  label: string;
};

type Props = {
  steps: Step[];
  currentStep: number;
};

export const Stepper = ({ steps, currentStep }: Props) => {
  return (
    <View style={styles.container}>
      <View style={styles.stepsWrapper}>
        {/* Background Line */}
        <View style={styles.lineBackground} />
        
        {/* Progress Line */}
        <View 
          style={[
            styles.lineProgress, 
            { width: `${((currentStep - 1) / (steps.length - 1)) * 100}%` }
          ]} 
        />

        {steps.map((step) => {
          const isActive = step.id === currentStep;
          const isCompleted = step.id < currentStep;

          return (
            <View key={step.id} style={styles.stepContainer}>
              <View
                style={[
                  styles.circle,
                  isActive && styles.circleActive,
                  isCompleted && styles.circleCompleted,
                ]}
              >
                <Text
                  style={[
                    styles.stepNumber,
                    isActive && styles.stepNumberActive,
                    isCompleted && styles.stepNumberCompleted,
                  ]}
                >
                  {isCompleted ? '✓' : step.id}
                </Text>
              </View>
              <Text
                style={[
                  styles.label,
                  isActive && styles.labelActive,
                  isCompleted && styles.labelCompleted,
                ]}
              >
                {step.label}
              </Text>
            </View>
          );
        })}
      </View>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    paddingVertical: 20,
    backgroundColor: '#fff',
    borderRadius: 16,
    marginBottom: 16,
    shadowColor: '#000',
    shadowOffset: { width: 0, height: 1 },
    shadowOpacity: 0.05,
    shadowRadius: 2,
    elevation: 2,
  },
  stepsWrapper: {
    flexDirection: 'row',
    alignItems: 'flex-start',
    justifyContent: 'space-between',
    paddingHorizontal: 20,
    position: 'relative',
  },
  stepContainer: {
    alignItems: 'center',
    width: 60, // Fixed width for each step container to keep spacing even
    zIndex: 1,
  },
  circle: {
    width: 30,
    height: 30,
    borderRadius: 15,
    backgroundColor: '#fff',
    borderWidth: 2,
    borderColor: '#e2e8f0',
    alignItems: 'center',
    justifyContent: 'center',
    marginBottom: 6,
  },
  circleActive: {
    borderColor: '#6366f1',
    backgroundColor: '#6366f1',
  },
  circleCompleted: {
    borderColor: '#6366f1',
    backgroundColor: '#6366f1',
  },
  stepNumber: {
    fontSize: 12,
    fontWeight: 'bold',
    color: '#94a3b8',
  },
  stepNumberActive: {
    color: '#fff',
  },
  stepNumberCompleted: {
    color: '#fff',
  },
  label: {
    fontSize: 9,
    fontWeight: 'bold',
    color: '#94a3b8',
    textTransform: 'uppercase',
    textAlign: 'center',
  },
  labelActive: {
    color: '#6366f1',
  },
  labelCompleted: {
    color: '#6366f1',
  },
  lineBackground: {
    position: 'absolute',
    top: 15, // Half of circle height (30/2)
    left: 50, // approx center of first circle (20 padding + 60/2)
    right: 50, // approx center of last circle
    height: 2,
    backgroundColor: '#e2e8f0',
    zIndex: 0,
  },
  lineProgress: {
    position: 'absolute',
    top: 15,
    left: 50,
    // Width is controlled by style prop
    height: 2,
    backgroundColor: '#6366f1',
    zIndex: 0,
    maxWidth: '75%', // approx distance between first and last circle centers
  },
});
