import * as React from "react";

import { cn } from "@/lib/utils";

type TableSize = "large" | "medium" | "small";

interface TableContextValue {
  size: TableSize;
}

const TableContext = React.createContext<TableContextValue>({
  size: "medium",
});

const useTableContext = () => React.useContext(TableContext);

const tableSizeStyles = {
  large: {
    head: "p-4 text-base",
    cell: "p-4 text-base",
  },

  medium: {
    head: "px-2 py-3 text-sm",
    cell: "px-2 py-3 text-sm",
  },

  small: {
    head: "p-2 text-xs",
    cell: "p-2 text-xs",
  },
};

export interface TableProps
  extends React.HTMLAttributes<HTMLTableElement> {
  size?: TableSize;
}

const Table = React.forwardRef<HTMLTableElement, TableProps>(
  ({ className, size = "medium", ...props }, ref) => (
    <TableContext.Provider value={{ size }}>
      <div className="relative w-full overflow-auto">
        <table
          ref={ref}
          className={cn("w-full caption-bottom", className)}
          {...props}
        />
      </div>
    </TableContext.Provider>
  )
);

Table.displayName = "Table";

const TableHeader = React.forwardRef<
  HTMLTableSectionElement,
  React.HTMLAttributes<HTMLTableSectionElement>
>(({ className, ...props }, ref) => (
  <thead
    ref={ref}
    className={cn("[&_tr]:border-b bg-muted", className)}
    {...props}
  />
));

TableHeader.displayName = "TableHeader";

const TableBody = React.forwardRef<
  HTMLTableSectionElement,
  React.HTMLAttributes<HTMLTableSectionElement>
>(({ className, ...props }, ref) => (
  <tbody
    ref={ref}
    className={cn("[&_tr:last-child]:border-0", className)}
    {...props}
  />
));

TableBody.displayName = "TableBody";

const TableFooter = React.forwardRef<
  HTMLTableSectionElement,
  React.HTMLAttributes<HTMLTableSectionElement>
>(({ className, ...props }, ref) => (
  <tfoot
    ref={ref}
    className={cn(
      "border-t bg-muted/50 font-medium [&>tr]:last:border-b-0",
      className
    )}
    {...props}
  />
));

TableFooter.displayName = "TableFooter";

const TableRow = React.forwardRef<
  HTMLTableRowElement,
  React.HTMLAttributes<HTMLTableRowElement>
>(({ className, ...props }, ref) => {
  useTableContext();

  return (
    <tr
      ref={ref}
      className={cn(
        "border-b transition-colors hover:bg-muted/50 data-[state=selected]:bg-muted",
        className
      )}
      {...props}
    />
  );
});

TableRow.displayName = "TableRow";

const TableHead = React.forwardRef<
  HTMLTableCellElement,
  React.ThHTMLAttributes<HTMLTableCellElement>
>(({ className, ...props }, ref) => {
  const { size } = useTableContext();

  return (
    <th
      ref={ref}
      className={cn(
        "text-left align-middle font-semibold text-muted-foreground [&:has([role=checkbox])]:pr-0",
        tableSizeStyles[size].head,
        className
      )}
      {...props}
    />
  );
});

TableHead.displayName = "TableHead";

const TableCell = React.forwardRef<
  HTMLTableCellElement,
  React.TdHTMLAttributes<HTMLTableCellElement>
>(({ className, ...props }, ref) => {
  const { size } = useTableContext();

  return (
    <td
      ref={ref}
      className={cn(
        "align-middle [&:has([role=checkbox])]:pr-0",
        tableSizeStyles[size].cell,
        className
      )}
      {...props}
    />
  );
});

TableCell.displayName = "TableCell";

const TableCaption = React.forwardRef<
  HTMLTableCaptionElement,
  React.HTMLAttributes<HTMLTableCaptionElement>
>(({ className, ...props }, ref) => (
  <caption
    ref={ref}
    className={cn("mt-4 text-sm text-muted-foreground", className)}
    {...props}
  />
));

TableCaption.displayName = "TableCaption";

export {
  Table,
  TableHeader,
  TableBody,
  TableFooter,
  TableHead,
  TableRow,
  TableCell,
  TableCaption,
};
