'use client';

import {ChartCard} from '@/shared/components/charts/ChartCard';
import {BarChartView} from '@/shared/components/charts/BarChartView';
import {formatCompact} from '@/shared/utils/format';
import type {RewardUsagePoint} from '@/features/dashboard/types/dashboard';
import type {Resource} from '@/shared/hooks/useResource';

/**
 * Claimed vs used, per reward.
 *
 * The gap between the two bars is the whole point: a reward claimed 700 times
 * and redeemed 130 is an outstanding liability and a broken offer, not a
 * success. Plotting the usage *rate* alone would hide the volume; plotting
 * volume alone would hide the failure. So both bars, side by side.
 */
export function RewardUsageChart({
  resource,
}: {
  resource: Resource<RewardUsagePoint[]>;
}) {
  return (
    <ChartCard
      title="Reward usage"
      subtitle="Claimed vs actually redeemed — the gap is unspent liability"
      resource={resource}
    >
      {(rows) => (
        <BarChartView
          data={rows}
          xKey="name"
          yFormat={formatCompact}
          series={[
            {key: 'claimed', label: 'Claimed'},
            {key: 'used', label: 'Redeemed'},
          ]}
        />
      )}
    </ChartCard>
  );
}
