'use client';

/**
 * Works / Catalogue Dashboard — Catalogue composition and performance.
 *
 * Displays:
 * - KPI card: total Works
 * - Pie chart: Works by genre
 * - Bar chart: Works by rights type (controlled vs uncontrolled)
 * - Horizontal bar chart: top 10 Works by income
 * - Line chart: Works added over time (monthly)
 * - Bar chart: writer split distribution
 * - DateRangeFilter at the top
 *
 * Requirements: 5.1, 5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.8
 */

import { useWorksData } 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 WorksPage() {
  const { data, isLoading } = useWorksData();
  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, byGenre, byRightsType, topByIncome, addedOverTime, writerSplitDistribution } = data;

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

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

      {/* Charts */}
      <ChartGrid>
        {/* Pie chart: Works by genre */}
        <ChartCard title={t.dashboard.worksByGenre}>
          <PieChartWidget data={byGenre} />
        </ChartCard>

        {/* Bar chart: Works by rights type */}
        <ChartCard title={t.dashboard.worksByRightsType}>
          <BarChartWidget
            data={byRightsType}
            xKey="name"
            bars={[{ dataKey: 'value', color: chartColors.primary, name: 'Works' }]}
          />
        </ChartCard>

        {/* Horizontal bar chart: top 10 Works by income */}
        <ChartCard title={t.dashboard.top10WorksByIncome}>
          <BarChartWidget
            data={topByIncome}
            xKey="name"
            bars={[{ dataKey: 'value', color: chartColors.primary, name: t.dashboard.totalIncome }]}
            layout="horizontal"
            yAxisFormatter={fmtAxis}
          />
        </ChartCard>

        {/* Line chart: Works added over time */}
        <ChartCard title={t.dashboard.worksAddedOverTime}>
          <LineChartWidget
            data={addedOverTime}
            xKey="month"
            lines={[{ dataKey: 'value', color: chartColors.primary, name: 'Works Added' }]}
          />
        </ChartCard>

        {/* Bar chart: writer split distribution */}
        <ChartCard title={t.dashboard.writerSplitDistribution}>
          <BarChartWidget
            data={writerSplitDistribution}
            xKey="name"
            bars={[{ dataKey: 'value', color: chartColors.primary, name: 'Splits' }]}
          />
        </ChartCard>
      </ChartGrid>
    </div>
  );
}
