import React from "react";
import {
  Table,
  TableContainer,
  Tbody,
  Td,
  Th,
  Thead,
  Tr,
  Skeleton,
  TableCaption,
  Radio,
  RadioGroup,
  CheckboxGroup,
  Checkbox,
} from "@chakra-ui/react";
import EmptySearchList from "../EmptySearchList";
import { TABLE_PAGINATION } from "../../Constants/Paginations";

const NormalTable = ({
  data,
  isLoading,
  tableHeadRow,
  emptyMessage,
  centered,
  total,
  showRadioButton, // New prop for showing the radio button
  selectedRadio,
  setSelectedRadio, // State for managing radio button selection
}) => {
  const columnWidth =
    data && data[0]
      ? `${(100 / Object.keys(data[0]).length).toFixed(2)}%`
      : "auto";

  const handleRadioChange = (value) => {
    setSelectedRadio(value);
  };

  return (
    <TableContainer overflowX={"auto"} className="h-auto w-100 table-scroll">
      {data?.length === 0 ? (
        <EmptySearchList message={emptyMessage} />
      ) : (
        <Table size="sm">
          <TableCaption p={total ? 0 : null}>
            {total ? total : "OptiFii v1.0.0"}
          </TableCaption>
          <Thead bg="#6311cb37">
            <Tr>
              {tableHeadRow.map((heading, index) => (
                <Th
                  color={"purple.900"}
                  textAlign={
                    tableHeadRow.length - 1 === index || centered
                      ? "center"
                      : "left"
                  }
                  key={index}
                  p={4}
                  whiteSpace="normal"
                  wordBreak="normal"
                  overflowWrap="normal"
                  textTransform={"none"}
                >
                  {/* Conditionally render radio button in the heading */}
                  {showRadioButton && heading === "Select" ? (
                    <CheckboxGroup value={selectedRadio}>
                      <Checkbox isDisabled /> {/* Disabled radio button in header */}
                    </CheckboxGroup>
                  ) : (
                    isLoading ? <Skeleton height="20px" /> : heading
                  )}
                </Th>
              ))}
            </Tr>
          </Thead>
          <Tbody className="web-text-small">
            {isLoading
              ? Array.from({ length: TABLE_PAGINATION?.size }).map(
                  (_, index) => (
                    <Tr bg={index % 2 === 0 ? "" : "#6311cb14"} key={index}>
                      {tableHeadRow.map((_, i) => (
                        <Td
                          key={i}
                          style={{
                            whiteSpace: "nowrap",
                            textOverflow: "ellipsis",
                          }}
                          className="web-text-small"
                        >
                          <Skeleton height="20px" mb={1} mt={1} />
                        </Td>
                      ))}
                    </Tr>
                  )
                )
              : data?.map((item, index) => (
                  <Tr bg={index % 2 === 0 ? "" : "#6311cb14"} key={index}>
                    {tableHeadRow.map((heading, i) => (
                      <Td
                        textAlign={
                          tableHeadRow?.length - 1 === i || centered
                            ? "center"
                            : "left"
                        }
                        color={"gray.600"}
                        key={i}
                        fontWeight={500}
                        style={{
                          whiteSpace: "nowrap",
                          textOverflow: "ellipsis",
                        }}
                        className="web-text-small"
                      >
                        {/* Conditionally render radio button in the table body */}
                        {showRadioButton && heading === "Select" ? (
                          <CheckboxGroup
                            value={selectedRadio}
                            onChange={handleRadioChange}
                          >
                            <Checkbox bg={'#fff'} value={item.id} /> {/* Dynamic radio buttons */}
                          </CheckboxGroup>
                        ) : (
                          item[heading]
                        )}
                      </Td>
                    ))}
                  </Tr>
                ))}
          </Tbody>
        </Table>
      )}
    </TableContainer>
  );
};

export default NormalTable;
