'use client';

/**
 * Costs Dashboard — Cost trends, types, and cost-to-income ratio analysis.
 *
 * Displays:
 * - 2 KPI cards: total costs, cost-to-income ratio
 * - Pie chart: costs by type (administration, ownership-purchase, other)
 * - Line chart: cost trends over time (monthly)
 * - Bar chart: costs by currency
 * - Line chart: cost-to-income ratio over time (dual series: costs + income + ratio)
 * - DateRangeFilter at the top
 *
 * Requirements: 11.1, 11.2, 11.3, 11.4, 11.5, 11.6, 11.7
 */

import { useCostsData } from '@/lib/api/queries';
import { useTranslation } from '@/lib/hooks/useTranslation';
import { formatPercentage } 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 CostsPage() {
  const { data, isLoading } = useCostsData();
  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, byType, trends, byCurrency, ratioOverTime } = data;

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

      {/* KPI Cards */}
      <KpiGrid>
        <KpiCard title={t.dashboard.totalCosts} value={fmtCurrency(kpis.totalCosts)} />
        <KpiCard
          title={t.dashboard.costToIncomeRatio}
          value={formatPercentage(kpis.costToIncomeRatio)}
        />
      </KpiGrid>

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

        {/* Line chart: cost trends over time */}
        <ChartCard title={t.dashboard.costTrends}>
          <LineChartWidget
            data={trends}
            xKey="month"
            lines={[{ dataKey: 'value', color: chartColors.primary, name: t.dashboard.totalCosts }]}
            yAxisFormatter={fmtAxis}
          />
        </ChartCard>

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

        {/* Line chart: cost-to-income ratio over time (dual series) */}
        <ChartCard title={t.dashboard.costRatioOverTime}>
          <LineChartWidget
            data={ratioOverTime}
            xKey="month"
            lines={[
              { dataKey: 'costs', color: chartColors.primary, name: t.dashboard.totalCosts },
              { dataKey: 'income', color: chartColors.quaternary, name: t.dashboard.totalIncome },
              { dataKey: 'ratio', color: chartColors.quinary, name: t.dashboard.costToIncomeRatio },
            ]}
            showLegend
            yAxisFormatter={fmtAxis}
          />
        </ChartCard>
      </ChartGrid>
    </div>
  );
}
