'use client';

/**
 * Transactions Dashboard — Transaction volume, types, and financial flow.
 *
 * Displays:
 * - 2 KPI cards: total Transaction count, total Transaction amount
 * - Line chart: transaction volume over time (monthly count)
 * - Pie chart: transactions by type (royalty, fee, advance, adjustment)
 * - Bar chart: transaction amounts by currency
 * - Bar chart: monthly transaction totals (sum of amounts)
 * - DateRangeFilter at the top
 *
 * Requirements: 10.1, 10.2, 10.3, 10.4, 10.5, 10.6, 10.7
 */

import { useTransactionsData } from '@/lib/api/queries';
import { useTranslation } from '@/lib/hooks/useTranslation';
import { formatNumber } from '@/lib/utils/format';
import { useCurrencyFormatter, 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 TransactionsPage() {
  const { data, isLoading } = useTransactionsData();
  const t = useTranslation();
  const fmtCurrency = useCurrencyFormatter();
  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, volumeOverTime, byType, byCurrency, monthlyTotals } = data;

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

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

      {/* Charts */}
      <ChartGrid>
        {/* Line chart: transaction volume over time */}
        <ChartCard title={t.dashboard.transactionsOverTime}>
          <LineChartWidget
            data={volumeOverTime}
            xKey="month"
            lines={[{ dataKey: 'value', color: chartColors.primary, name: t.dashboard.totalTransactions }]}
          />
        </ChartCard>

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

        {/* Bar chart: by currency */}
        <ChartCard title={t.dashboard.transactionsByCurrency}>
          <BarChartWidget
            data={byCurrency}
            xKey="name"
            bars={[{ dataKey: 'value', color: chartColors.primary, name: t.dashboard.totalAmount }]}
            yAxisFormatter={fmtAxis}
          />
        </ChartCard>

        {/* Bar chart: monthly totals */}
        <ChartCard title={t.dashboard.amountByType}>
          <BarChartWidget
            data={monthlyTotals}
            xKey="month"
            bars={[{ dataKey: 'value', color: chartColors.primary, name: t.dashboard.totalAmount }]}
            yAxisFormatter={fmtAxis}
          />
        </ChartCard>
      </ChartGrid>
    </div>
  );
}
