"use client";

import { ResponsiveContainer, BarChart, Bar, XAxis, YAxis, Tooltip, CartesianGrid, LabelList } from "recharts";
import { useT } from "@/shared/i18n/client";
import type { DataQualityWeek } from "@/domain/entities/data-quality";

export default function WeeklyReliableChart({ weeks }: { weeks: DataQualityWeek[] }) {
  const t = useT();
  const copy = t.dataQuality.weeks;

  if (weeks.length === 0) {
    return <p className="py-10 text-center text-sm text-muted-foreground">{copy.empty}</p>;
  }

  const data = weeks.map((w) => ({
    label: copy.week(w.week.slice(-2)),
    share: w.reliable_share ?? 0,
    km: w.km,
  }));

  return (
    <div className="h-56 w-full">
      <ResponsiveContainer width="100%" height="100%">
        <BarChart data={data} margin={{ top: 20, right: 8, left: -16, bottom: 0 }}>
          <CartesianGrid strokeDasharray="3 3" vertical={false} stroke="var(--border)" />
          <XAxis dataKey="label" tickLine={false} axisLine={false} fontSize={12} />
          <YAxis domain={[0, 100]} tickLine={false} axisLine={false} fontSize={12} tickFormatter={(v) => `${v}%`} />
          <Tooltip
            cursor={{ fill: "var(--muted)" }}
            formatter={(value, name) =>
              name === "share" ? [`${value}%`, copy.reliableShare] : [value, copy.km]
            }
          />
          <Bar dataKey="share" fill="var(--color-blue-500, #3b82f6)" radius={[4, 4, 0, 0]} maxBarSize={40}>
            <LabelList dataKey="share" position="top" fontSize={11} formatter={(v) => `${v}%`} />
          </Bar>
        </BarChart>
      </ResponsiveContainer>
    </div>
  );
}
