// Segment.tsx
// This component renders a single column segment (bar) for a column chart, with support for accessibility and testID.

import React from 'react';
import { Rect } from 'react-native-svg';
import { useTheme } from '../../../theme';

interface SegmentProps {
  xCenter: number;
  y: number;
  height: number;
  color?: string;
  value?: number;
  xLabel: string;
  seriesLabel: string;
  testID: string;
  onPress?: () => void;
}

/**
 * Renders a single column segment (bar) for a column chart.
 * Applies theming, segment gap, and accessibility label.
 * Used by StackedSegment and other chart content components.
 */
const Segment = ({
  xCenter,
  y,
  height,
  color,
  value,
  xLabel,
  seriesLabel,
  testID,
  onPress,
}: SegmentProps) => {
  const theme = useTheme();
  const { segmentGap } = theme.__hd__.columnChart.space;
  const width = theme.__hd__.columnChart.sizes.columnWidth;

  // Apply segment gap logic
  const minBarHeight = 10;
  let adjustedHeight = height - segmentGap;
  if (height > 0 && adjustedHeight < minBarHeight) {
    adjustedHeight = minBarHeight;
  } else if (height <= 0) {
    adjustedHeight = 0;
  }
  const adjustedY = y + segmentGap / 2;
  const x = xCenter - width / 2;

  return (
    <Rect
      x={x}
      y={adjustedY}
      width={width}
      height={adjustedHeight}
      rx={width / 2}
      fill={color}
      accessibilityLabel={`Column segment: value ${value}, x-label ${xLabel}, series ${seriesLabel}, color ${color}`}
      testID={testID}
      onPress={onPress}
    />
  );
};

export default React.memo(Segment);
