"use client";

import React from "react";
import { useTranslation } from "react-i18next";

export interface TableColumn {
  label: string | React.ReactElement;
  key: string;
  type?: string;
  className?: string;
  customClassName?: string;
}

/**
 * `Row` is the caller's own row type.
 *
 * Generic rather than `Record<string, any>`. The table really is generic — the
 * column list is data, so a cell's key is only known at runtime — but `any`
 * meant each caller's rows were never checked against anything, which is how
 * three row types drifted from the resources they describe. A type parameter
 * gives the same flexibility while keeping `renderCell` typed against the
 * caller's actual row, and `Record<string, unknown>` would not work here:
 * an interface without an index signature is not assignable to it.
 */
interface TableProps<Row> {
  columns: TableColumn[];
  data: Row[];
  renderCell?: (column: TableColumn, rowData: Row) => React.ReactElement;
}

function Table<Row>({ columns, data, renderCell }: TableProps<Row>) {
  const { t } = useTranslation();

  return (
    <div className="table-container w-full overflow-x-auto">
      <table className="min-w-max w-full border-collapse">
        {/* Table Head */}
        <thead>
          <tr>
            {columns.map((col, index) => (
              <th
                key={index}
                className={`border-b pb-3 text-left rtl:text-right text-[#181822CC]/80 text-xl mobilescreen:text-sm mobilescreen:pr-8 font-semibold whitespace-nowrap pr-8 ${
                  col.customClassName || ""
                }`}
              >
                {col.label}
              </th>
            ))}
          </tr>
        </thead>

        {/* Table Body */}
        <tbody>
          {data.length === 0 ? (
            <tr>
              <td
                colSpan={columns.length}
                className="border-b p-3 text-[#181822CC]/80 text-lg xss:text-sm font-normal text-center"
              >
                {t("COMMON.NO_DATA_AVAILABLE")}
              </td>
            </tr>
          ) : (
            data.map((row, rowIndex) => (
              <tr key={rowIndex}>
                {columns.map((col, colIndex) => (
                  <td
                    key={colIndex}
                    className={`border-b py-3 text-[#181822CC]/80 text-lg font-normal whitespace-nowrap mobilescreen:text-sm mobilescreen:pr-8 pr-8 not-italic no-underline text-inherit cursor-pointer ${
                      col.customClassName || ""
                    }`}
                  >
                    {/* The default cell reads by column key, which is only
                        known at runtime — narrowed to an index read and
                        stringified rather than typed `any`. */}
                    {renderCell
                      ? renderCell(col, row)
                      : String(
                          (row as Record<string, unknown>)[col.key] ?? "-"
                        )}
                  </td>
                ))}
              </tr>
            ))
          )}
        </tbody>
      </table>
    </div>
  );
}

export default Table;
