"use client"

import { Bar, BarChart, CartesianGrid, XAxis, YAxis } from "recharts"

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

export const description = "Perbandingan total penjualan dan belanja per bulan."

const chartConfig = {
  sales: {
    label: "Penjualan",
    color: "var(--chart-1)",
  },
  purchase: {
    label: "Belanja",
    color: "var(--chart-2)",
  },
} satisfies ChartConfig

export function RevenueVsPurchasesChart({ 
    data,
    title = "Penjualan vs Belanja",
    description = "Perbandingan total nilai penjualan dan belanja stok barang."
}: { 
    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">
          <BarChart accessibilityLayer data={data}>
            <CartesianGrid vertical={false} />
            <XAxis
              dataKey="month"
              tickLine={false}
              tickMargin={10}
              axisLine={false}
              tickFormatter={(value) => value.slice(0, 3)}
            />
            <YAxis
              tickLine={false}
              axisLine={false}
              tickMargin={8}
              tickFormatter={formatShortCurrency}
              width={60}
            />
            <ChartTooltip
              cursor={false}
              content={
                <ChartTooltipContent
                  indicator="dashed"
                  formatter={(value, name) => [
                    formatCurrency(Number(value)),
                    ` - ${chartConfig[name as keyof typeof chartConfig]?.label ?? name}`,
                  ]}
                />
              }
            />
            <Bar dataKey="sales" fill="var(--color-sales)" radius={4} />
            <Bar dataKey="purchase" fill="var(--color-purchase)" radius={4} />
            <ChartLegend content={<ChartLegendContent />} />
          </BarChart>
        </ChartContainer>
      </CardContent>
    </Card>
  )
}
