import {
  DataTableDateCell,
  DataTableNumberCell,
  type DataTableColDef,
} from "@/components/ui/data-table";
import { formatDocumentCellValue } from "@/lib/document-file";
import {
  GovernmentAgencyCell,
  IssueCountCell,
  MeetingDocumentCell,
  SummaryTextCell,
  SummaryTitleCell,
} from "@/features/ministry/meeting-summary/components/meeting-summary-table-cells";
import type { MeetingSummaryRow } from "@/features/ministry/meeting-summary/meeting-summary-data";
import {
  ActionCell,
  StatusBadge,
} from "@/features/pswg/meeting-summary/components/table/meeting-summary-table-cells";
import {
  MEETING_REQUEST_COLUMN_WIDTH,
  withFixedColumnWidths,
} from "@/features/pswg/meeting-summary/components/table/meeting-summary-table-config";
import {
  meetingSummaryText,
  type MeetingSummaryLanguage,
} from "@/features/pswg/meeting-summary/meeting-summary-i18n";

export function getMeetingSummaryTableColumns(
  language: MeetingSummaryLanguage = "en",
  // Base path for the "View Detail" link, e.g. "/cdc-gpsf/meeting-summary".
  detailBasePath?: string,
): DataTableColDef<MeetingSummaryRow>[] {
  const text = meetingSummaryText[language] ?? meetingSummaryText.en;

  return withFixedColumnWidths([
    {
      field: "id",
      headerName: text.columns.id,
      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: text.columns.summaryTitle,
      width: 285,
      minWidth: 160,
      headerAlign: "center",
      sortable: false,
      renderCell: (params) => (
        <SummaryTitleCell value={params.row.summaryTitle} />
      ),
    },
    {
      field: "meetingDate",
      headerName: text.columns.meetingDate,
      width: 146,
      minWidth: 130,
      headerAlign: "center",
      sortable: false,
      renderCell: (params) => (
        <DataTableDateCell value={params.row.meetingDate} align="left" />
      ),
    },
    {
      field: "issueCount",
      headerName: text.columns.issueCount,
      width: 163,
      minWidth: 88,
      headerAlign: "center",
      sortable: false,
      renderCell: (params) => (
        <IssueCountCell count={Number(params.value ?? 0)} />
      ),
    },
    {
      field: "meetingRequest",
      headerName: text.columns.meetingRequest,
      width: MEETING_REQUEST_COLUMN_WIDTH,
      headerAlign: "center",
      sortable: false,
      renderCell: (params) => (
        <MeetingDocumentCell label={params.row.meetingRequest} wrap />
      ),
    },
    {
      field: "meetingSummary",
      headerName: text.columns.meetingSummary,
      width: 184,
      minWidth: 140,
      headerAlign: "center",
      sortable: false,
      renderCell: (params) => (
        <SummaryTextCell
          value={formatDocumentCellValue(params.row.meetingSummary)}
          fontWeight={400}
        />
      ),
    },
    {
      field: "governmentAgency",
      headerName: text.columns.governmentAgency,
      width: 184,
      minWidth: 160,
      headerAlign: "center",
      sortable: false,
      renderCell: (params) => (
        <GovernmentAgencyCell
          name={params.row.governmentAgency}
          logo={params.row.governmentAgencyLogo}
        />
      ),
    },
    {
      field: "pswg",
      headerName: text.columns.pswg,
      width: 202,
      minWidth: 120,
      headerAlign: "center",
      sortable: false,
      renderCell: (params) => (
        <SummaryTextCell value={params.row.pswg} fontWeight={400} />
      ),
    },
    {
      field: "status",
      headerName: text.columns.status,
      width: 150,
      minWidth: 120,
      align: "center",
      headerAlign: "center",
      sortable: false,
      renderCell: (params) => (
        <StatusBadge status={params.row.status} language={language} />
      ),
    },
    {
      field: "meetingPswg",
      headerName: text.columns.meetingPswg,
      width: 220,
      minWidth: 160,
      headerAlign: "center",
      sortable: false,
      renderCell: (params) => (
        <SummaryTextCell value={params.row.meetingPswg} fontWeight={400} />
      ),
    },
    {
      field: "action",
      headerName: text.columns.action,
      width: 154,
      minWidth: 100,
      align: "center",
      headerAlign: "center",
      sortable: false,
      filterable: false,
      disableColumnMenu: true,
      renderCell: (params) => (
        <ActionCell
          id={params.row.id}
          language={language}
          detailBasePath={detailBasePath}
        />
      ),
    },
  ]);
}
