"use client"

import { CartesianGrid, Line, LineChart, XAxis, YAxis } from "recharts"

import {
  Card,
  CardContent,
  CardDescription,
  CardHeader,
  CardTitle
} from "@/components/ui/card"
import {
  ChartContainer,
  ChartTooltip,
  ChartTooltipContent,
  type ChartConfig,
} from "@/components/ui/chart"
import { formatCurrency, formatShortCurrency } from "@/lib/formatters"

export const description = "Tren rata-rata nilai pesanan (AOV) per bulan."

const chartConfig = {
  aov: {
    label: "AOV",
    color: "var(--chart-1)",
  },
} satisfies ChartConfig

export function AovTrendChart({
  data,
  title = "Tren AOV",
  description = "Rata-rata nilai belanja per pesanan pelanggan setiap bulannya."
}: {
  data: any[],
  title?: string,
  description?: string
}) {
  return (
    <Card>
      <CardHeader>
        <CardTitle>{title}</CardTitle>
        <CardDescription>{description}</CardDescription>
      </CardHeader>
      <CardContent>
        <ChartContainer config={chartConfig} className="aspect-auto h-[300px] w-full">
          <LineChart
            accessibilityLayer
            data={data}
            margin={{
              left: 12,
              right: 12,
            }}
          >
            <CartesianGrid vertical={false} />
            <XAxis
              dataKey="month"
              tickLine={false}
              axisLine={false}
              tickMargin={8}
              tickFormatter={(value) => value.slice(0, 3)}
            />
            <YAxis
              tickLine={false}
              axisLine={false}
              tickMargin={8}
              tickFormatter={formatShortCurrency}
              width={60}
            />
            <ChartTooltip
              cursor={false}
              content={
                <ChartTooltipContent
                  hideLabel
                  formatter={(value, name) => [
                    formatCurrency(Number(value)),
                    ` - ${chartConfig[name as keyof typeof chartConfig]?.label ?? name}`,
                  ]}
                />
              }
            />
            <Line
              dataKey="aov"
              type="natural"
              stroke="var(--color-aov)"
              strokeWidth={2}
              dot={{
                fill: "var(--color-aov)",
              }}
              activeDot={{
                r: 6,
              }}
            />
          </LineChart>
        </ChartContainer>
      </CardContent>
    </Card>
  )
}
