"use client";

import { useMemo } from "react";

import { alpha, useTheme } from "@mui/material/styles";
import type { SxProps, Theme } from "@mui/material/styles";

import { DataTable } from "@/components/ui/data-table";
import { useAppLanguage } from "@/components/providers/app-language-provider";
import type { MeetingSummaryRow } from "@/features/ministry/meeting-summary/meeting-summary-data";
import { getMeetingSummaryFont } from "@/features/ministry/meeting-summary/meeting-summary-i18n";
import { MEETING_SUMMARY_TABLE_CONFIG } from "@/features/pswg/meeting-summary/components/table/meeting-summary-table-config";
import { getMeetingSummaryTableColumns } from "@/features/pswg/meeting-summary/components/table/meeting-summary-table-columns";
import { normalizeMeetingSummaryLanguage } from "@/features/pswg/meeting-summary/meeting-summary-i18n";

type Props = {
  rows: MeetingSummaryRow[];
  loading?: boolean;
  // Base path for the row "View Detail" link, e.g. "/cdc-gpsf/meeting-summary".
  detailBasePath?: string;
};

export function MeetingSummaryTable({
  rows,
  loading = false,
  detailBasePath,
}: Props) {
  const theme = useTheme();
  const isDark = theme.palette.mode === "dark";
  const { language } = useAppLanguage();
  const meetingSummaryLanguage = normalizeMeetingSummaryLanguage(language);

  const columns = useMemo(
    () => getMeetingSummaryTableColumns(meetingSummaryLanguage, detailBasePath),
    [meetingSummaryLanguage, detailBasePath],
  );

  const dataGridSx = useMemo<SxProps<Theme>>(() => {
    const headerBg = isDark ? alpha("#ffffff", 0.06) : "#ddedfb";
    const bodyBg = isDark ? "#101828" : "#ffffff";
    const rowHoverBg = isDark ? alpha("#ffffff", 0.04) : "#f9fafb";
    const borderColor = isDark ? "#263244" : "#f5f5f5";

    return {
      border: "none",
      width: "100%",

      "& .MuiDataGrid-columnHeadersInner": {
        minWidth: MEETING_SUMMARY_TABLE_CONFIG.minWidth,
      },

      "& .MuiDataGrid-virtualScrollerContent": {
        minWidth: MEETING_SUMMARY_TABLE_CONFIG.minWidth,
      },

      fontFamily: getMeetingSummaryFont(
        meetingSummaryLanguage === "kh" ? "km" : "en",
      ),

      "& .MuiDataGrid-columnHeaders": {
        backgroundColor: headerBg,
        borderBottom: `1px solid ${borderColor}`,
      },

      "& .MuiDataGrid-columnHeader": {
        px: 2,
        backgroundColor: headerBg,
      },

      "& .MuiDataGrid-columnHeaderTitleContainer": {
        overflow: "visible",
        justifyContent: "center",
      },

      "& .MuiDataGrid-columnHeaderTitle": {
        fontSize: 12,
        fontWeight: 400,
        whiteSpace: "nowrap",
        overflow: "visible",
        textOverflow: "clip",
        color: isDark ? alpha("#ffffff", 0.72) : "#717680",
      },

      "& .MuiDataGrid-cell": {
        px: 2,
        py: 0,
        color: isDark ? "#ffffff" : "#181d27",
        borderColor,
        borderBottom: `1px solid ${borderColor}`,
        bgcolor: bodyBg,
        display: "flex",
        alignItems: "center",
      },

      "& .MuiDataGrid-row": {
        bgcolor: bodyBg,
      },

      "& .MuiDataGrid-row:hover": {
        bgcolor: rowHoverBg,
      },

      "& .MuiDataGrid-cell[data-field='action']": {
        justifyContent: "center",
      },

      "& .MuiDataGrid-withBorderColor": {
        borderColor,
      },

      "& .MuiDataGrid-main": {
        overflow: "hidden",
      },

      "& .MuiDataGrid-virtualScroller": {
        overflowX: "auto !important",
        overflowY: "auto !important",
      },

      "& .MuiDataGrid-cell:focus, & .MuiDataGrid-cell:focus-within": {
        outline: "none",
      },

      "& .MuiDataGrid-columnHeader:focus, & .MuiDataGrid-columnHeader:focus-within":
        {
          outline: "none",
        },
    };
  }, [isDark, meetingSummaryLanguage]);

  return (
    <DataTable
      fillAvailableHeight
      rows={rows}
      columns={columns}
      getRowId={(row) => row.id}
      loading={loading}
      emptyMessage="No meeting summaries match the selected filters."
      pageSize={MEETING_SUMMARY_TABLE_CONFIG.pageSize}
      rowHeight={MEETING_SUMMARY_TABLE_CONFIG.rowHeight}
      columnHeaderHeight={MEETING_SUMMARY_TABLE_CONFIG.headerHeight}
      enableColumnMenu
      dataGridSx={dataGridSx}
      paperSx={{
        width: "100%",
        minWidth: 0,
        mt: 2,
        overflow: "hidden",
        border: isDark
          ? `1px solid ${alpha("#ffffff", 0.1)}`
          : "1px solid var(--ms-border, #f5f5f5)",
      }}
    />
  );
}
