'use client';

/**
 * Payees Dashboard — Earnings distribution and payee composition.
 *
 * Displays:
 * - KPI card: total Payees
 * - Horizontal bar chart: top 10 Payees by earnings
 * - Pie chart: Payees by type (publisher, sub-publisher, administrator, individual)
 * - Line chart: payment amounts over time (monthly)
 * - Bar chart: Payees by territory
 * - DateRangeFilter at the top
 *
 * Requirements: 7.1, 7.2, 7.3, 7.4, 7.5, 7.6, 7.7
 */

import { usePayeesData } from '@/lib/api/queries';
import { useTranslation } from '@/lib/hooks/useTranslation';
import { formatNumber } from '@/lib/utils/format';
import { useCurrencyAxisFormatter } from '@/lib/hooks/useCurrencyFormatter';
import { chartColors } from '@/lib/constants/theme';
import DateRangeFilter from '@/components/dashboard/DateRangeFilter';
import KpiGrid from '@/components/dashboard/KpiGrid';
import KpiCard from '@/components/dashboard/KpiCard';
import ChartGrid from '@/components/dashboard/ChartGrid';
import ChartCard from '@/components/charts/ChartCard';
import LineChartWidget from '@/components/charts/LineChartWidget';
import PieChartWidget from '@/components/charts/PieChartWidget';
import BarChartWidget from '@/components/charts/BarChartWidget';

export default function PayeesPage() {
  const { data, isLoading } = usePayeesData();
  const t = useTranslation();
  const fmtAxis = useCurrencyAxisFormatter();

  if (isLoading || !data) {
    return (
      <div className="flex items-center justify-center h-64">
        <p className="text-divisi-text-secondary text-sm">{t.dashboard.loading}</p>
      </div>
    );
  }

  const { kpis, topByEarnings, byType, paymentOverTime, byTerritory } = data;

  return (
    <div className="space-y-6">
      {/* Date Range Filter */}
      <DateRangeFilter />

      {/* KPI Cards */}
      <KpiGrid>
        <KpiCard title={t.dashboard.totalPayees} value={formatNumber(kpis.totalPayees)} />
      </KpiGrid>

      {/* Charts */}
      <ChartGrid>
        {/* Horizontal bar chart: top 10 Payees by earnings */}
        <ChartCard title={t.dashboard.top10PayeesByEarnings}>
          <BarChartWidget
            data={topByEarnings}
            xKey="name"
            bars={[{ dataKey: 'value', color: chartColors.primary, name: t.dashboard.earningsDistribution }]}
            layout="horizontal"
            yAxisFormatter={fmtAxis}
          />
        </ChartCard>

        {/* Pie chart: Payees by type */}
        <ChartCard title={t.dashboard.payeesByType}>
          <PieChartWidget data={byType} />
        </ChartCard>

        {/* Line chart: payment amounts over time */}
        <ChartCard title={t.dashboard.earningsDistribution}>
          <LineChartWidget
            data={paymentOverTime}
            xKey="month"
            lines={[{ dataKey: 'value', color: chartColors.primary, name: t.dashboard.totalAmount }]}
            yAxisFormatter={fmtAxis}
          />
        </ChartCard>

        {/* Bar chart: Payees by territory */}
        <ChartCard title={t.dashboard.payeesByTerritory}>
          <BarChartWidget
            data={byTerritory}
            xKey="name"
            bars={[{ dataKey: 'value', color: chartColors.primary, name: t.dashboard.totalPayees }]}
          />
        </ChartCard>
      </ChartGrid>
    </div>
  );
}
