import {
  DataTableNumberCell,
  type DataTableColDef,
} from "@/components/ui/data-table";
import { DocumentFileCell } from "@/components/ui/document-file-name";
import { formatDocumentCellValue } from "@/lib/document-file";

import {
  IssueListAgencyCell,
  IssueListStatusCell,
  IssueListTextCell,
} from "@/features/ministry/meeting-summary/components/view-meeting-summary/issue-list-table-cells";
import type { ViewMeetingSummaryIssueRow } from "@/features/ministry/meeting-summary/components/view-meeting-summary/view-meeting-summary-data";

import { MeetingSummaryListOfIssueUpdateButton } from "./meeting-summary-list-of-issue-table-cells";

type GetMeetingSummaryListOfIssueTableColumnsOptions = {
  onUpdateIssue?: (issue: ViewMeetingSummaryIssueRow) => void;
};

export function getMeetingSummaryListOfIssueTableColumns({
  onUpdateIssue,
}: GetMeetingSummaryListOfIssueTableColumnsOptions = {}): DataTableColDef<ViewMeetingSummaryIssueRow>[] {
  return [
    {
      field: "id",
      headerName: "No.",
      width: 52,
      minWidth: 52,
      align: "center",
      headerAlign: "center",
      sortable: false,
      filterable: false,
      disableColumnMenu: true,
      renderCell: (params) => (
        <DataTableNumberCell
          value={
            params.api.getRowIndexRelativeToVisibleRows(params.row.id) + 1
          }
        />
      ),
    },
    {
      field: "wgName",
      headerName: "WG Name",
      width: 280,
      minWidth: 280,
      sortable: false,
      renderCell: (params) => (
        <IssueListTextCell value={params.row.wgName} wrap />
      ),
    },
    {
      field: "issue",
      headerName: "Issue",
      width: 241,
      minWidth: 241,
      sortable: false,
      showTooltip: (params) => params.row.issue,
      renderCell: (params) => <IssueListTextCell value={params.row.issue} />,
    },
    {
      field: "category",
      headerName: "Category of Issue",
      width: 292,
      minWidth: 292,
      sortable: false,
      showTooltip: (params) => params.row.category,
      renderCell: (params) => <IssueListTextCell value={params.row.category} />,
    },
    {
      field: "issueDescription",
      headerName: "Issue Description",
      width: 501,
      minWidth: 501,
      sortable: false,
      showTooltip: (params) => params.row.issueDescription,
      renderCell: (params) => (
        <IssueListTextCell value={params.row.issueDescription} />
      ),
    },
    {
      field: "recommendation",
      headerName: "Recommendation",
      width: 440,
      minWidth: 440,
      sortable: false,
      showTooltip: (params) => params.row.recommendation,
      renderCell: (params) => (
        <IssueListTextCell value={params.row.recommendation} />
      ),
    },
    {
      field: "rgcDecision",
      headerName: "RGC Decision",
      width: 440,
      minWidth: 440,
      sortable: false,
      showTooltip: (params) => params.row.rgcDecision,
      renderCell: (params) => <IssueListTextCell value={params.row.rgcDecision} />,
    },
    {
      field: "nextStep",
      headerName: "Next Step",
      width: 215,
      minWidth: 215,
      sortable: false,
      renderCell: (params) => (
        <IssueListTextCell value={params.row.nextStep} compact />
      ),
    },
    {
      field: "remark",
      headerName: "Remark",
      width: 215,
      minWidth: 215,
      sortable: false,
      renderCell: (params) => (
        <IssueListTextCell value={params.row.remark} compact />
      ),
    },
    {
      field: "issueReference",
      headerName: "Issue Reference",
      width: 215,
      minWidth: 215,
      sortable: false,
      showTooltip: (params) =>
        formatDocumentCellValue(params.row.issueReference),
      renderCell: (params) => (
        <DocumentFileCell value={params.row.issueReference} compact />
      ),
    },
    {
      field: "status",
      headerName: "Status",
      width: 215,
      minWidth: 215,
      sortable: false,
      renderCell: (params) => (
        <IssueListStatusCell label={params.row.status} />
      ),
    },
    {
      field: "primaryAgency",
      headerName: "Gov't primary Agency",
      width: 215,
      minWidth: 215,
      sortable: false,
      renderCell: (params) => (
        <IssueListAgencyCell
          name={params.row.primaryAgency}
          logo={params.row.primaryAgencyLogo}
        />
      ),
    },
    {
      field: "secondAgency",
      headerName: "Gov't second Agency",
      width: 215,
      minWidth: 215,
      sortable: false,
      renderCell: (params) => (
        <IssueListAgencyCell
          name={params.row.secondAgency}
          logo={params.row.secondAgencyLogo}
        />
      ),
    },
    {
      field: "thirdAgency",
      headerName: "Gov't third Agency",
      width: 215,
      minWidth: 215,
      sortable: false,
      renderCell: (params) => (
        <IssueListAgencyCell
          name={params.row.thirdAgency}
          logo={params.row.thirdAgencyLogo}
        />
      ),
    },
    {
      field: "fourthAgency",
      headerName: "Gov't fourth Agency",
      width: 215,
      minWidth: 215,
      sortable: false,
      renderCell: (params) => (
        <IssueListAgencyCell
          name={params.row.fourthAgency}
          logo={params.row.fourthAgencyLogo}
        />
      ),
    },
    {
      field: "fifthAgency",
      headerName: "Gov't fifth Agency",
      width: 215,
      minWidth: 215,
      sortable: false,
      renderCell: (params) => (
        <IssueListAgencyCell
          name={params.row.fifthAgency}
          logo={params.row.fifthAgencyLogo}
        />
      ),
    },
    {
      field: "issueEscalation",
      headerName: "Issue Escalation",
      width: 215,
      minWidth: 215,
      sortable: false,
      renderCell: (params) => (
        <IssueListTextCell value={params.row.issueEscalation} compact />
      ),
    },
    {
      field: "action",
      headerName: "Action",
      width: 150,
      minWidth: 150,
      sortable: false,
      filterable: false,
      disableColumnMenu: true,
      renderCell: (params) => (
        <MeetingSummaryListOfIssueUpdateButton
          issue={params.row}
          onUpdate={onUpdateIssue}
        />
      ),
    },
  ];
}
