"use client"

import { TrendingUp } from "lucide-react"
import { CartesianGrid, Line, LineChart, XAxis, YAxis } from "recharts"

import {
  Card,
  CardContent,
  CardDescription,
  CardFooter,
  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 }: { data: any[] }) {
  return (
    <Card>
      <CardHeader>
        <CardTitle>Tren AOV</CardTitle>
        <CardDescription>Rata-rata nilai belanja per pesanan</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) => [
                        formatCurrency(Number(value)),
                        chartConfig.aov.label
                    ]}
                />
              }
            />
            <Line
              dataKey="aov"
              type="natural"
              stroke="var(--color-aov)"
              strokeWidth={2}
              dot={{
                fill: "var(--color-aov)",
              }}
              activeDot={{
                r: 6,
              }}
            />
          </LineChart>
        </ChartContainer>
      </CardContent>
      <CardFooter className="flex-col items-start gap-2 text-sm">
        <div className="flex gap-2 leading-none font-medium">
          Menampilkan tren kualitas belanja pelanggan <TrendingUp className="h-4 w-4" />
        </div>
        <div className="leading-none text-muted-foreground">
          Berdasarkan data operasional Januari - Desember
        </div>
      </CardFooter>
    </Card>
  )
}
