'use client';

import { useState } from 'react';
import { ColumnChart } from '@/components/admin/reports/ReportCharts';
import { money } from '@/components/admin/marketing/ui';

export interface DailyInsight { date: string; impressions: number; clicks: number; spend: number; conversions: number; conversion_value: number }

const METRICS = [
  { key: 'spend', label: 'Spend' },
  { key: 'clicks', label: 'Clicks' },
  { key: 'impressions', label: 'Views' },
  { key: 'conversions', label: 'Purchases' },
] as const;

// Daily campaign performance — reuses the dependency-free ColumnChart from
// the Reports page.
export default function CampaignInsightsChart({ daily, currency }: { daily: DailyInsight[]; currency: string | null }) {
  const [metric, setMetric] = useState<(typeof METRICS)[number]['key']>('spend');
  return (
    <div>
      <div className="flex flex-wrap gap-1.5 mb-2">
        {METRICS.map((m) => (
          <button key={m.key} type="button" onClick={() => setMetric(m.key)} className="rounded-full px-3 py-1 text-xs font-medium cursor-pointer"
            style={metric === m.key ? { background: 'var(--color-cta)', color: '#fff' } : { background: 'var(--color-surface-alt)', color: 'var(--color-text-secondary)' }}>
            {m.label}
          </button>
        ))}
      </div>
      <ColumnChart
        ariaLabel={`Daily ${metric}`}
        points={daily.map((d) => ({ key: d.date, label: d.date.slice(5), title: d.date, value: d[metric] }))}
        formatValue={(n) => (metric === 'spend' ? money(n, currency) : n.toLocaleString())}
      />
    </div>
  );
}
