"use client";

import { Bar, BarChart, CartesianGrid, Cell, ResponsiveContainer, Tooltip, XAxis, YAxis } from "recharts";

import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { type ClientType, clients } from "@/data/clients";
import {
  SS_CHART_CURSOR_FILL,
  SS_CHART_GRID_STROKE,
  SS_CHART_TICK_FILL,
  SS_CHART_TOOLTIP_STYLE,
} from "@/lib/silisuite-chart-styles";

const TYPE_ORDER: ClientType[] = ["Corporate", "Individual", "Trust", "Partnership"];

const COLORS: Record<ClientType, string> = {
  Corporate: "#6366f1",
  Individual: "#3b82f6",
  Trust: "#f59e0b",
  Partnership: "#10b981",
};

export function OverviewClientDistribution() {
  const chartData = TYPE_ORDER.map((type) => ({
    type,
    count: clients.filter((c) => c.type === type).length,
  }));

  return (
    <Card>
      <CardHeader className="pb-2">
        <CardTitle className="ss-heading text-base">Clients by Type (legacy)</CardTitle>
      </CardHeader>
      <CardContent className="pt-1">
        <ResponsiveContainer width="100%" height={220}>
          <BarChart data={chartData} barCategoryGap="24%">
            <CartesianGrid strokeDasharray="3 3" stroke={SS_CHART_GRID_STROKE} vertical={false} />
            <XAxis dataKey="type" tick={{ fontSize: 12, fill: SS_CHART_TICK_FILL }} axisLine={false} tickLine={false} />
            <YAxis
              allowDecimals={false}
              tick={{ fontSize: 12, fill: SS_CHART_TICK_FILL }}
              axisLine={false}
              tickLine={false}
              width={28}
            />
            <Tooltip cursor={{ fill: SS_CHART_CURSOR_FILL }} contentStyle={SS_CHART_TOOLTIP_STYLE} />
            <Bar dataKey="count" radius={[4, 4, 0, 0]}>
              {chartData.map((row) => (
                <Cell key={row.type} fill={COLORS[row.type]} />
              ))}
            </Bar>
          </BarChart>
        </ResponsiveContainer>
      </CardContent>
    </Card>
  );
}
