import React from 'react';
import { View, Text, StyleSheet } from 'react-native';
import Svg, { Rect, Circle, Line, Text as SvgText } from 'react-native-svg';

interface PipelineFlowData {
  label: string;
  value: number;
  color: string;
}

interface PipelineFlowProps {
  data: PipelineFlowData[];
  colors: any;
  fonts: any;
}

const PipelineFlow: React.FC<PipelineFlowProps> = ({ 
  data, 
  colors, 
  fonts 
}) => {
  const total = data.reduce((sum, item) => sum + item.value, 0);
  const maxValue = Math.max(...data.map(item => item.value));
  
  if (total === 0) {
    return (
      <View style={styles.container}>
        <Text style={[styles.noDataText, { color: colors.textLight, fontFamily: fonts.regular }]}>
          No Pipeline Data
        </Text>
      </View>
    );
  }

  const containerWidth = 300;
  const stageHeight = 50;
  const circleRadius = 12;
  const lineWidth = 40;
  const startX = 30;
  const textOffset = 60;

  return (
    <View style={styles.container}>
      <Svg width={containerWidth} height={data.length * stageHeight}>
        {data.map((stage, index) => {
          const y = index * stageHeight + stageHeight / 2;
          const x = startX + (index * (circleRadius * 2 + lineWidth));
          const percentage = (stage.value / maxValue) * 100;
          const barWidth = (percentage / 100) * 80 + 20; // Min width 20, max 100
          
          return (
            <React.Fragment key={index}>
              {/* Stage Circle */}
              <Circle
                cx={x}
                cy={y}
                r={circleRadius}
                fill={stage.color}
                stroke={colors.surface}
                strokeWidth={2}
              />
              
              {/* Stage Value inside circle */}
              <SvgText
                x={x}
                y={y + 4}
                fontSize="10"
                fontFamily={fonts.bold}
                fill={colors.surface}
                textAnchor="middle"
                alignmentBaseline="middle"
              >
                {stage.value}
              </SvgText>
              
              {/* Progress Bar */}
              <Rect
                x={textOffset}
                y={y - 8}
                width={barWidth}
                height={16}
                fill={stage.color}
                fillOpacity={0.3}
                rx={8}
                ry={8}
              />
              
              {/* Progress Fill */}
              <Rect
                x={textOffset}
                y={y - 8}
                width={barWidth}
                height={16}
                fill={stage.color}
                rx={8}
                ry={8}
              />
              
              {/* Stage Label */}
              <SvgText
                x={textOffset + 10}
                y={y + 4}
                fontSize="11"
                fontFamily={fonts.medium}
                fill={colors.text}
                textAnchor="start"
                alignmentBaseline="middle"
              >
                {stage.label}
              </SvgText>
              
              {/* Percentage */}
              <SvgText
                x={textOffset + barWidth - 10}
                y={y + 4}
                fontSize="9"
                fontFamily={fonts.regular}
                fill={colors.textLight}
                textAnchor="end"
                alignmentBaseline="middle"
              >
                {((stage.value / total) * 100).toFixed(0)}%
              </SvgText>
              
              {/* Connecting Line (except for last item) */}
              {index < data.length - 1 && (
                <Line
                  x1={x + circleRadius}
                  y1={y}
                  x2={x + circleRadius + lineWidth}
                  y2={y}
                  stroke={colors.border}
                  strokeWidth={2}
                  strokeDasharray="4,4"
                />
              )}
            </React.Fragment>
          );
        })}
      </Svg>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    alignItems: 'flex-start',
    justifyContent: 'center',
    paddingVertical: 10,
    paddingHorizontal: 5,
  },
  noDataText: {
    fontSize: 14,
    textAlign: 'center',
    fontStyle: 'italic',
  },
});

export default PipelineFlow;
