import {
  DataTableDateCell,
  DataTableNumberCell,
  type DataTableColDef,
} from "@/components/ui/data-table";

import type {
  MeetingSummaryRow,
  MeetingSummaryStatus,
} from "../../meeting-summary-data";
import { meetingSummaryI18n, type UiLang } from "../../meeting-summary-i18n";
import {
  MEETING_REQUEST_COLUMN_WIDTH,
  withFixedColumnWidths,
} from "./meeting-summary-table-config";
import {
  GovernmentAgencyCell,
  IssueCountCell,
  MeetingDocumentCell,
  SummaryActionCell,
  SummaryStatusCell,
  SummaryTextCell,
  SummaryTitleCell,
} from "../meeting-summary-table-cells";

export function getMeetingSummaryTableColumns(
  lang: UiLang = "en",
  handlers?: {
    onSubmitReportSuccess?: () => void;
    onRowUpdated?: (id: number, status: MeetingSummaryStatus) => void;
  },
): DataTableColDef<MeetingSummaryRow>[] {
  const t = meetingSummaryI18n[lang];

  return withFixedColumnWidths([
    {
      field: "id",
      headerName: t.columns.no,
      width: 72,
      minWidth: 72,
      align: "center",
      headerAlign: "center",
      sortable: false,
      filterable: false,
      disableColumnMenu: true,
      renderCell: (params) => (
        <DataTableNumberCell
          value={
            params.api.getRowIndexRelativeToVisibleRows(params.row.id) + 1
          }
        />
      ),
    },
    {
      field: "summaryTitle",
      headerName: t.columns.summaryTitle,
      width: 285,
      minWidth: 160,
      sortable: false,
      renderCell: (params) => (
        <SummaryTitleCell value={params.row.summaryTitle} />
      ),
    },
    {
      field: "meetingDate",
      headerName: t.columns.meetingDate,
      width: 146,
      minWidth: 130,
      sortable: false,
      renderCell: (params) => (
        <DataTableDateCell
          value={params.row.meetingDate}
          align="left"
          showTooltip={false}
        />
      ),
    },
    {
      field: "issueCount",
      headerName: t.columns.issueCount,
      width: 163,
      minWidth: 88,
      sortable: false,
      renderCell: (params) => (
        <IssueCountCell count={Number(params.value ?? 0)} />
      ),
    },
    {
      field: "meetingRequest",
      headerName: t.columns.meetingRequest,
      width: MEETING_REQUEST_COLUMN_WIDTH,
      align: "center",
      headerAlign: "center",
      sortable: false,
      renderCell: (params) => (
        <MeetingDocumentCell label={params.row.meetingRequest} />
      ),
    },
    {
      field: "meetingSummary",
      headerName: t.columns.meetingSummary,
      width: MEETING_REQUEST_COLUMN_WIDTH,
      align: "center",
      headerAlign: "center",
      sortable: false,
      renderCell: (params) => (
        <MeetingDocumentCell label={params.row.meetingSummary} />
      ),
    },
    {
      field: "governmentAgency",
      headerName: t.columns.governmentAgency,
      width: 184,
      minWidth: 160,
      sortable: false,
      renderCell: (params) => (
        <GovernmentAgencyCell
          name={params.row.governmentAgency}
          logo={params.row.governmentAgencyLogo}
        />
      ),
    },
    {
      field: "pswg",
      headerName: t.columns.pswg,
      width: 202,
      minWidth: 120,
      sortable: false,
      renderCell: (params) => (
        <SummaryTextCell value={params.row.pswg} fontWeight={400} />
      ),
    },
    {
      field: "status",
      headerName: t.columns.status,
      width: 150,
      minWidth: 120,
      align: "center",
      headerAlign: "left",
      sortable: false,
      renderCell: (params) => <SummaryStatusCell status={params.row.status} />,
    },
    {
      field: "meetingPswg",
      headerName: t.columns.meetingPswg,
      width: 220,
      minWidth: 160,
      sortable: false,
      renderCell: (params) => (
        <SummaryTextCell value={params.row.meetingPswg} fontWeight={400} />
      ),
    },
    {
      field: "action",
      headerName: t.columns.action,
      width: 154,
      minWidth: 100,
      align: "center",
      headerAlign: "center",
      sortable: false,
      filterable: false,
      disableColumnMenu: true,
      renderCell: (params) => (
        <SummaryActionCell
          row={params.row}
          lang={lang}
          onSubmitReportSuccess={handlers?.onSubmitReportSuccess}
          onRowUpdated={handlers?.onRowUpdated}
        />
      ),
    },
  ]);
}
