// packages/core/src/components/data-table/data-table.props.ts
import { z } from "zod/v4";
import type { JSX } from "solid-js";
import type { ComponentMeta } from "../../meta";

export const DataTableColumnSchema = z.object({
  id: z.string().describe("Unique column identifier"),
  header: z.string().describe("Column header label"),
  sortable: z.boolean().optional().describe("Whether the column is sortable"),
  width: z.number().optional().describe("Fixed column width in pixels"),
  minWidth: z.number().optional().describe("Minimum column width in pixels"),
});

export type SortDirection = "asc" | "desc" | "none";

export interface DataTableColumn<T = unknown> extends z.infer<typeof DataTableColumnSchema> {
  /** Renders the cell content for a given row. */
  cell: (row: T, index: number) => JSX.Element;
  /** Custom sort comparator. Falls back to localeCompare on JSON-stringified values. */
  sortFn?: (a: T, b: T) => number;
  /** Custom per-column filter. Falls back to global JSON.stringify includes. */
  filterFn?: (row: T, filterValue: string) => boolean;
}

export const DataTableRootPropsSchema = z.object({
  pageSize: z.number().optional().describe("Number of rows per page. Defaults to 10"),
  defaultPage: z.number().optional().describe("Initial page index (0-based, uncontrolled)"),
  page: z.number().optional().describe("Controlled page index (0-based)"),
  filterValue: z.string().optional().describe("Controlled global filter string"),
  defaultFilterValue: z.string().optional().describe("Initial filter value (uncontrolled)"),
  enableRowSelection: z.boolean().optional().describe("Whether row selection is enabled"),
  enableMultiRowSelection: z.boolean().optional().describe("Whether multiple rows can be selected"),
});

export interface DataTableRootProps<T = unknown> extends z.infer<typeof DataTableRootPropsSchema> {
  /** The data rows to display. */
  data: T[];
  /** Column definitions. */
  columns: DataTableColumn<T>[];
  /** Called when the page changes. */
  onPageChange?: (page: number) => void;
  /** Called when the filter value changes. */
  onFilterChange?: (value: string) => void;
  /** Called when selection changes. */
  onSelectionChange?: (selectedIndices: Set<number>) => void;
  children: JSX.Element;
}

export const DataTableMeta: ComponentMeta = {
  name: "DataTable",
  description: "Feature-rich data table with sorting, filtering, pagination, and row selection",
  parts: ["Root", "Header", "HeaderCell", "Body", "Row", "Cell", "Pagination"] as const,
  requiredParts: ["Root", "Header", "Body", "Pagination"] as const,
} as const;
