"use client"

import { GitCommitVertical, 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 } from "@/lib/formatters"

export const description = "Menampilkan volume transaksi per bulan."

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

export function TransactionVolumeChart({ data }: { data: any[] }) {
  return (
    <Card>
      <CardHeader>
        <CardTitle>Volume Transaksi</CardTitle>
        <CardDescription>Total pesanan masuk per bulan</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}
              width={30}
            />
            <ChartTooltip
              cursor={false}
              content={
                <ChartTooltipContent
                  formatter={(value, name, item) => {
                    if (name === "count") {
                        return (
                            <div className="flex flex-col gap-1">
                                <div className="flex items-center gap-2">
                                    <div className="h-2 w-2 rounded-full bg-[var(--color-count)]" />
                                    <span className="text-muted-foreground">Transaksi:</span>
                                    <span className="font-bold">{value}</span>
                                </div>
                                <div className="flex items-center gap-2">
                                    <div className="h-2 w-2 rounded-full bg-[var(--color-aov)]" />
                                    <span className="text-muted-foreground">AOV:</span>
                                    <span className="font-bold">{formatCurrency(item.payload.aov)}</span>
                                </div>
                            </div>
                        )
                    }
                    return null;
                  }}
                />
              }
            />
            <Line
              dataKey="count"
              type="natural"
              stroke="var(--color-count)"
              strokeWidth={2}
              dot={({ cx, cy, payload }) => {
                if (cx == null || cy == null) {
                  return null
                }

                const r = 24

                return (
                  <GitCommitVertical
                    key={payload.month}
                    x={cx - r / 2}
                    y={cy - r / 2}
                    width={r}
                    height={r}
                    fill="hsl(var(--background))"
                    stroke="var(--color-count)"
                  />
                )
              }}
            />
          </LineChart>
        </ChartContainer>
      </CardContent>
    </Card>
  )
}
