import { useMemo } from 'react';
import Chart from 'react-apexcharts';

export const LineChart = ({
  series = [],
  categories = [],
  title = '',
  height = 350,
  colors = ['#3B82F6', '#10B981', '#F59E0B', '#EF4444', '#8B5CF6'],
  showLegend = true,
  showToolbar = true,
  className = '',
  ...props
}) => {
  const chartOptions = useMemo(() => ({
    chart: {
      type: 'line',
      height: height,
      toolbar: {
        show: showToolbar,
        tools: {
          download: true,
          selection: true,
          zoom: true,
          zoomin: true,
          zoomout: true,
          pan: true,
          reset: true,
        },
      },
    },
    colors: colors,
    stroke: {
      curve: 'smooth',
      width: 2,
    },
    xaxis: {
      categories: categories,
      labels: {
        style: {
          colors: '#6B7280',
        },
      },
    },
    yaxis: {
      labels: {
        style: {
          colors: '#6B7280',
        },
      },
    },
    legend: {
      show: showLegend,
      position: 'top',
      horizontalAlign: 'center',
    },
    grid: {
      borderColor: '#E5E7EB',
      strokeDashArray: 4,
    },
    tooltip: {
      theme: 'light',
    },
    dataLabels: {
      enabled: false,
    },
  }), [categories, colors, height, showLegend, showToolbar]);

  return (
    <div className={className}>
      {title && (
        <h3 className="text-lg font-semibold text-gray-900 mb-4">{title}</h3>
      )}
      <Chart
        options={chartOptions}
        series={series}
        type="line"
        height={height}
        {...props}
      />
    </div>
  );
};

export default LineChart;

