"use client";
import type { ReportEntry, ReportKind } from "../services/accountReport";
import { formatHoursMinutes } from "@/features/shared/clientReviewPolicy";
import { useTranslation } from "react-i18next";
import { reportEntryLearners, reportRowKey, reportRoleLabelKey, reportTableColumns } from "../reportEntryPolicy";
export default function AccountReportTable({ entries, kind, section, language }: {
  entries: ReportEntry[]; kind: ReportKind; section: "volunteer" | "development" | "sponsorship"; language: "ar" | "en";
}) {
  const { t } = useTranslation();
  const individual = kind === "individual";
  const volunteering = section === "volunteer";
  const currency = language === "ar" ? "د.ك" : "KD";
  const money = (value: number | null) => value == null ? "—" : `${Number(value).toFixed(3)} ${currency}`;
  const columns = reportTableColumns(kind, section);
  const dateLabel = (value?: string | null) => {
    if (!value) return "—";
    const date = new Date(`${value.slice(0, 10)}T12:00:00+03:00`);
    return Number.isFinite(date.getTime()) ? new Intl.DateTimeFormat(language === "ar" ? "ar-KW-u-nu-latn" : "en-GB", { timeZone: "Asia/Kuwait", day: "numeric", month: "short", year: "numeric" }).format(date) : "—";
  };
  const cell = "p-3 text-center text-sm align-middle";
  return <div className="overflow-x-auto rounded-xl border bg-white"><table className="w-full min-w-[600px] border-collapse">
    <thead className="bg-primary-5 text-white"><tr>{columns.map(key => <th className={`${cell} ${key === columns[0] ? "!text-start" : ""}`} key={key}>{t(`COMMON.${key}`, { lng: language })}</th>)}</tr></thead>
    <tbody>{entries.length === 0 ? <tr><td className={`${cell} text-center`} colSpan={columns.length}>{t("COMMON.NO_DATA", { lng: language })}</td></tr> : entries.map(row => <tr key={reportRowKey(row)} className="border-b last:border-0">
      <td className={`${cell} !text-start`}>{row.title}</td>
      {section !== "sponsorship" && <td className={cell}>{reportRoleLabelKey(row.role) ? t(`COMMON.${reportRoleLabelKey(row.role)}`, { lng: language }) : "—"}</td>}
      {section === "sponsorship" && <td className={cell}>{row.publisher_name || "—"}</td>}
      <td className={cell}>{row.type}</td>
      {individual && volunteering && <td className={cell}>{row.publisher_name || "—"}</td>}
      {volunteering ? <>{!individual && <><td className={cell}>{row.beneficiaries ?? "—"}</td><td className={cell}>{row.attendees ?? "—"}</td></>}
        <td className={cell} dir="ltr">{row.actual_hours == null ? "—" : formatHoursMinutes(row.actual_hours)}</td>
        {!individual && <td className={cell}>{money(row.economic_impact)}</td>}
      </> : section === "sponsorship" ? <><td className={cell}>{row.beneficiaries ?? "—"}</td><td className={cell}>{reportEntryLearners(row) ?? "—"}</td></> : <>{!individual && <td className={cell}>{reportEntryLearners(row) ?? "—"}</td>}

      </>}
      <td className={`${cell} whitespace-nowrap`}><span className="block">{dateLabel(row.date)}</span>{row.end_date && row.end_date !== row.date && <span className="mt-1 block text-secondary-102">{dateLabel(row.end_date)}</span>}</td>
    </tr>)}</tbody>
  </table></div>;
}
