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

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

interface CompactPipelineProps {
  data: CompactPipelineData[];
  colors: any;
  fonts: any;
}

const CompactPipeline: React.FC<CompactPipelineProps> = ({ 
  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 = 320;
  const barHeight = 24;
  const labelWidth = 80;
  const valueWidth = 40;
  const barWidth = containerWidth - labelWidth - valueWidth - 60; // 60 for padding

  return (
    <View style={styles.container}>
      <Svg width={containerWidth} height={data.length * 35}>
        {data.map((stage, index) => {
          const y = index * 35 + 5;
          const percentage = total > 0 ? (stage.value / total) * 100 : 0;
          const barFillWidth = maxValue > 0 ? (stage.value / maxValue) * barWidth : 0;
          
          return (
            <React.Fragment key={index}>
              {/* Stage Label */}
              <SvgText
                x={10}
                y={y + 16}
                fontSize="12"
                fontFamily={fonts.medium}
                fill={colors.text}
                textAnchor="start"
                alignmentBaseline="middle"
              >
                {stage.label.length > 10 ? stage.label.substring(0, 10) + '...' : stage.label}
              </SvgText>
              
              {/* Progress Bar Background */}
              <Rect
                x={labelWidth + 10}
                y={y}
                width={barWidth}
                height={barHeight}
                fill={colors.border}
                rx={4}
                ry={4}
              />
              
              {/* Progress Bar Fill */}
              <Rect
                x={labelWidth + 10}
                y={y}
                width={barFillWidth}
                height={barHeight}
                fill={stage.color}
                rx={4}
                ry={4}
              />
              
              {/* Value */}
              <SvgText
                x={labelWidth + barWidth + 20}
                y={y + 16}
                fontSize="12"
                fontFamily={fonts.bold}
                fill={colors.text}
                textAnchor="middle"
                alignmentBaseline="middle"
              >
                {stage.value}
              </SvgText>
              
              {/* Percentage */}
              <SvgText
                x={labelWidth + barWidth + 60}
                y={y + 16}
                fontSize="10"
                fontFamily={fonts.regular}
                fill={colors.textLight}
                textAnchor="middle"
                alignmentBaseline="middle"
              >
                {percentage.toFixed(0)}%
              </SvgText>
            </React.Fragment>
          );
        })}
      </Svg>
      
      {/* Summary Row */}
      <View style={styles.summaryRow}>
        <View style={[styles.summaryItem, { borderColor: colors.border }]}>
          <Text style={[styles.summaryLabel, { color: colors.textLight, fontFamily: fonts.regular }]}>
            Total Stages
          </Text>
          <Text style={[styles.summaryValue, { color: colors.text, fontFamily: fonts.bold }]}>
            {data.length}
          </Text>
        </View>
        <View style={[styles.summaryItem, { borderColor: colors.border }]}>
          <Text style={[styles.summaryLabel, { color: colors.textLight, fontFamily: fonts.regular }]}>
            Total Deals
          </Text>
          <Text style={[styles.summaryValue, { color: colors.text, fontFamily: fonts.bold }]}>
            {total}
          </Text>
        </View>
        <View style={[styles.summaryItem, { borderColor: colors.border }]}>
          <Text style={[styles.summaryLabel, { color: colors.textLight, fontFamily: fonts.regular }]}>
            Avg/Stage
          </Text>
          <Text style={[styles.summaryValue, { color: colors.text, fontFamily: fonts.bold }]}>
            {data.length > 0 ? Math.round(total / data.length) : 0}
          </Text>
        </View>
      </View>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    paddingVertical: 8,
    paddingHorizontal: 5,
  },
  noDataText: {
    fontSize: 14,
    textAlign: 'center',
    fontStyle: 'italic',
    paddingVertical: 20,
  },
  summaryRow: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    marginTop: 12,
    paddingTop: 8,
  },
  summaryItem: {
    flex: 1,
    alignItems: 'center',
    paddingVertical: 6,
    borderTopWidth: 1,
    marginHorizontal: 4,
  },
  summaryLabel: {
    fontSize: 10,
    marginBottom: 2,
  },
  summaryValue: {
    fontSize: 14,
    fontWeight: 'bold',
  },
});

export default CompactPipeline;
