import React, { useEffect, useMemo, useRef, useState } from "react";
import {
  Chart as ChartJS,
  CategoryScale,
  LinearScale,
  BarController,
  BarElement,
  Tooltip,
  Legend,
  type ChartOptions,
  type ChartData,
} from "chart.js";
import { Chart } from "react-chartjs-2";
import { useThemeVars } from "@/lib/theme-provider";
import { Card, CardContent, CardHeader, CardTitle, CardAction } from "./card";
import { Empty, EmptyDescription } from "./empty";
import { Skeleton } from "./skeleton";
import { cn } from "@/lib/utils";
import {
  hexToRgba,
  DATASET_ALPHAS,
  FALLBACK_TICK,
  FALLBACK_PRIMARY,
  CHART_SLICE_COUNT,
  CHART_PERIODS,
  formatTooltipDate,
  ChartLegendItem,
  ChartPeriodButton,
  type ChartPeriod,
  type ChartGranularity,
} from "./chart-shared";

ChartJS.register(
  CategoryScale,
  LinearScale,
  BarController,
  BarElement,
  Tooltip,
  Legend,
);

// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------

export interface IncomeDataset {
  /** Income source label, e.g. "Employment", "Rental", "Other" */
  label: string;
  /** One value per data point, aligned to the months array */
  data: number[];
}

export interface IncomeBarChartData {
  /** Display labels, e.g. ["Jul", "Aug"] for monthly or ["Mar 8", "Mar 9"] for daily */
  months: string[];
  /** Optional ISO date strings per point — used for the tooltip title */
  dates?: string[];
  datasets: IncomeDataset[];
}

export type IncomePeriod = ChartPeriod;
export type IncomeGranularity = ChartGranularity;

export interface IncomeBarChartProps {
  /** Full dataset — sliced to the selected period */
  incomeData: IncomeBarChartData | null;
  title?: string;
  /** Show or hide the chart legend */
  showLegend?: boolean;
  /** Show or hide X axis labels */
  showXAxis?: boolean;
  /** Show or hide Y axis labels */
  showYAxis?: boolean;
  /** Legend placement relative to chart */
  legendPosition?: "top" | "bottom";
  /** Default period selector value */
  defaultPeriod?: IncomePeriod;
  /**
   * Data granularity — controls available period buttons and slice counts.
   * "monthly" (default): shows 3M / 6M / 12M, slices by month count.
   * "daily": shows 1M / 3M / 6M / 12M, slices by day count (1M=30, 3M=90, etc.).
   */
  granularity?: IncomeGranularity;
  /** Chart canvas height in pixels */
  height?: number;
  /** Width of the card in pixels */
  width?: number | string;
  className?: string;
  /** Show skeleton loading state instead of the chart */
  isLoading?: boolean;
  /**
   * Show the internal period selector buttons (3M / 6M / 12M).
   * Set to `false` when the chart is driven by an external period control.
   * Defaults to `true`.
   */
  showPeriodSelector?: boolean;
}

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------

// ---------------------------------------------------------------------------
// Component
// ---------------------------------------------------------------------------

export function IncomeBarChart({
  incomeData,
  title = "Income",
  showLegend = true,
  showXAxis = true,
  showYAxis = true,
  legendPosition = "top",
  defaultPeriod = 6,
  granularity = "monthly",
  height = 280,
  width = "100%",
  className,
  isLoading = false,
  showPeriodSelector = true,
}: IncomeBarChartProps) {
  const periods = CHART_PERIODS[granularity];
  const [period, setPeriod] = useState<IncomePeriod>(defaultPeriod);

  // Reset period when granularity changes, but not on initial mount
  // (defaultPeriod handles the initial value).
  const isFirstRender = useRef(true);
  useEffect(() => {
    if (isFirstRender.current) {
      isFirstRender.current = false;
      return;
    }
    setPeriod(CHART_PERIODS[granularity][0]);
  }, [granularity]);

  const themeVars = useThemeVars();
  const brandPrimary: string =
    (themeVars["--theme-primary"] as string | undefined) || FALLBACK_PRIMARY;
  const fontFamily: string =
    (themeVars["--font-sans"] as string | undefined) || "Figtree, sans-serif";

  const sliced = useMemo<IncomeBarChartData | null>(() => {
    if (!incomeData?.months?.length || !incomeData.datasets.length) return null;
    const count = Math.min(
      CHART_SLICE_COUNT[granularity][period],
      incomeData.months.length,
    );
    const start = incomeData.months.length - count;
    return {
      months: incomeData.months.slice(start),
      dates: incomeData.dates?.slice(start),
      datasets: incomeData.datasets.map((ds) => ({
        ...ds,
        data: ds.data.slice(start),
      })),
    };
  }, [incomeData, period, granularity]);

  const datasetColors = useMemo(
    () =>
      sliced?.datasets.map((_, i) =>
        hexToRgba(brandPrimary, DATASET_ALPHAS[i % DATASET_ALPHAS.length]),
      ) ?? [],
    [sliced, brandPrimary],
  );

  const chartData = useMemo<ChartData<"bar">>(() => {
    if (!sliced) return { labels: [], datasets: [] };
    return {
      labels: sliced.months,
      datasets: sliced.datasets.map((ds, i) => ({
        label: ds.label,
        data: ds.data,
        backgroundColor: datasetColors[i],
        hoverBackgroundColor: hexToRgba(
          brandPrimary,
          Math.min(DATASET_ALPHAS[i % DATASET_ALPHAS.length] + 0.15, 1),
        ),
        borderWidth: 0,
        borderRadius: 0,
        borderSkipped: false,
        barPercentage: 0.75,
        categoryPercentage: 0.7,
        stack: "income",
      })),
    };
  }, [sliced, datasetColors, brandPrimary]);

  const options = useMemo<ChartOptions<"bar">>(
    () => ({
      responsive: true,
      maintainAspectRatio: false,
      animation: { duration: 800, easing: "easeOutQuart" },
      layout: { padding: 0 },
      plugins: {
        legend: { display: false },
        tooltip: {
          mode: "index",
          intersect: false,
          padding: 12,
          cornerRadius: 0,
          titleFont: { size: 12, weight: 600 },
          bodyFont: { size: 12, weight: 500 },
          callbacks: {
            title: (tooltipItems) => {
              const idx = tooltipItems[0]?.dataIndex;
              if (idx != null && sliced?.dates?.[idx]) {
                return formatTooltipDate(sliced.dates[idx], granularity);
              }
              return tooltipItems[0]?.label ?? "";
            },
            label: (ctx) => {
              const val = ctx.raw as number;
              if (val === 0) return;
              return ` ${ctx.dataset.label}: $${val.toLocaleString()}`;
            },
          },
        },
      },
      scales: {
        x: {
          display: showXAxis,
          stacked: true,
          grid: { display: false },
          border: { display: false },
          ticks: { font: { size: 12 }, color: FALLBACK_TICK },
        },
        y: {
          display: showYAxis,
          stacked: true,
          grid: { display: false },
          border: { display: false },
          ticks: {
            font: { size: 12 },
            color: FALLBACK_TICK,
            maxTicksLimit: 5,
            padding: 8,
            callback: (v) => `$${Number(v).toLocaleString()}`,
          },
        },
      },
    }),
    [showXAxis, showYAxis, sliced, granularity],
  );

  return (
    <Card
      className={cn("w-full py-4 sm:py-6 gap-2", className)}
      style={{ maxWidth: width, fontFamily }}
    >
      {(title || showPeriodSelector) && (
        <CardHeader className="px-3 sm:px-6">
          {title && <CardTitle className="text-overline">{title}</CardTitle>}
          {showPeriodSelector && (
            <CardAction>
              <div className="flex gap-0.5 sm:gap-1">
                {periods.map((p) => (
                  <ChartPeriodButton
                    key={p}
                    period={p}
                    active={period === p}
                    onClick={() => setPeriod(p)}
                  />
                ))}
              </div>
            </CardAction>
          )}
        </CardHeader>
      )}

      <CardContent className="px-3 sm:px-6">
        {isLoading ? (
          <Skeleton style={{ height, width: "100%" }} />
        ) : !sliced ? (
          <Empty className="flex-none p-4" style={{ height }}>
            <EmptyDescription>No data available</EmptyDescription>
          </Empty>
        ) : (
          <div className="flex flex-col gap-2">
            {showLegend && legendPosition === "top" && (
              <div className="flex flex-wrap gap-x-3 gap-y-1.5 pb-2">
                {sliced.datasets.map((ds, i) => (
                  <ChartLegendItem
                    key={ds.label}
                    label={ds.label}
                    color={datasetColors[i]}
                  />
                ))}
              </div>
            )}
            <div style={{ height, width: "100%", position: "relative" }}>
              <Chart
                key={brandPrimary}
                type="bar"
                data={chartData}
                options={options}
                aria-label={title}
              />
            </div>
            {showLegend && legendPosition === "bottom" && (
              <div className="flex flex-wrap gap-x-3 gap-y-1.5 pt-2">
                {sliced.datasets.map((ds, i) => (
                  <ChartLegendItem
                    key={ds.label}
                    label={ds.label}
                    color={datasetColors[i]}
                  />
                ))}
              </div>
            )}
          </div>
        )}
      </CardContent>
    </Card>
  );
}
