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

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

interface FunnelChartProps {
  data: FunnelChartData[];
  colors: any;
  fonts: any;
}

const FunnelChart: React.FC<FunnelChartProps> = ({ 
  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>
    );
  }

  // Increased dimensions for better spacing
  const funnelWidth = 280;
  const funnelHeight = 220;
  const segmentHeight = funnelHeight / data.length;
  const centerX = funnelWidth / 2;
  const textPadding = 20; // Space for text on sides

  const createFunnelPath = (index: number, value: number) => {
    const availableWidth = funnelWidth - (textPadding * 2);
    const width = (value / maxValue) * (availableWidth * 0.7) + (availableWidth * 0.15);
    const y = index * segmentHeight;
    const nextY = (index + 1) * segmentHeight;
    const nextWidth = index < data.length - 1 ? 
      (data[index + 1].value / maxValue) * (availableWidth * 0.7) + (availableWidth * 0.15) : 
      (availableWidth * 0.15);

    const leftX = centerX - width / 2;
    const rightX = centerX + width / 2;
    const nextLeftX = centerX - nextWidth / 2;
    const nextRightX = centerX + nextWidth / 2;

    return `M ${leftX} ${y} L ${rightX} ${y} L ${nextRightX} ${nextY} L ${nextLeftX} ${nextY} Z`;
  };

  return (
    <View style={styles.container}>
      <Svg width={funnelWidth} height={funnelHeight}>
        {data.map((item, index) => {
          const percentage = (item.value / total) * 100;
          const path = createFunnelPath(index, item.value);
          const textY = index * segmentHeight + segmentHeight / 2;
          
          return (
            <React.Fragment key={index}>
              <Path
                d={path}
                fill={item.color}
                fillOpacity={0.8}
              />
              {/* Label text */}
              <SvgText
                x={centerX}
                y={textY - 8}
                fontSize="11"
                fontFamily={fonts.medium}
                fill={colors.textLight}
                textAnchor="middle"
                alignmentBaseline="middle"
              >
                {item.label.length > 12 ? item.label.substring(0, 12) + '...' : item.label}
              </SvgText>
              {/* Value and percentage text */}
              <SvgText
                x={centerX}
                y={textY + 8}
                fontSize="9"
                fontFamily={fonts.regular}
                fill={colors.textLight}
                textAnchor="middle"
                alignmentBaseline="middle"
              >
                {item.value} ({percentage.toFixed(0)}%)
              </SvgText>
            </React.Fragment>
          );
        })}
      </Svg>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    alignItems: 'center',
    justifyContent: 'center',
    paddingVertical: 15,
    paddingHorizontal: 10,
    width: '100%',
  },
  noDataText: {
    fontSize: 14,
    textAlign: 'center',
    fontStyle: 'italic',
  },
});

export default FunnelChart;
