// Table/DataGrid Component Specification
// Framework-agnostic table component definition

import { Action, ScreenName } from '@/core/operations/permissions/permission-types';

export type TableSize = 'sm' | 'md' | 'lg';
export type SortDirection = 'asc' | 'desc' | null;
export type TableVariant = 'default' | 'striped' | 'bordered' | 'hoverable';

export interface TableColumn<T = unknown> {
  key: string;
  header: string;
  accessor?: (row: T) => unknown;
  render?: (row: T, value: unknown) => unknown;
  sortable?: boolean;
  filterable?: boolean;
  align?: 'left' | 'center' | 'right';
  width?: string;
  minWidth?: string;
  maxWidth?: string;
  
  // Role-based visibility
  visibleForRoles?: string[];
  hiddenForRoles?: string[];
  requiredAction?: Action;
  requiredModule?: string;
  
  // Styling
  className?: string;
  headerClassName?: string;
  cellClassName?: string;
}

export interface TableFilter {
  column: string;
  operator: 'equals' | 'contains' | 'startsWith' | 'endsWith' | 'greaterThan' | 'lessThan' | 'between';
  value: unknown;
}

export interface TableSort {
  column: string;
  direction: SortDirection;
}

export interface TablePagination {
  page: number;
  pageSize: number;
  total: number;
  showPageSizeSelector?: boolean;
  pageSizeOptions?: number[];
}

export interface TableProps<T = unknown> {
  // Data
  data: T[];
  columns: TableColumn<T>[];
  keyField?: string | ((row: T) => string);
  
  // Features
  sortable?: boolean;
  filterable?: boolean;
  paginated?: boolean;
  selectable?: boolean;
  expandable?: boolean;
  
  // Sorting
  defaultSort?: TableSort;
  onSort?: (sort: TableSort) => void;
  
  // Filtering
  filters?: TableFilter[];
  onFilter?: (filters: TableFilter[]) => void;
  globalSearch?: boolean;
  searchPlaceholder?: string;
  
  // Pagination
  pagination?: TablePagination;
  onPageChange?: (page: number) => void;
  onPageSizeChange?: (pageSize: number) => void;
  
  // Selection
  selectedRows?: T[];
  onSelectionChange?: (selected: T[]) => void;
  selectAllEnabled?: boolean;
  
  // Expansion
  expandedRows?: string[];
  onExpansionChange?: (expanded: string[]) => void;
  renderExpandedRow?: (row: T) => unknown;
  
  // Styling
  variant?: TableVariant;
  size?: TableSize;
  striped?: boolean;
  bordered?: boolean;
  hoverable?: boolean;
  fullWidth?: boolean;
  className?: string;
  
  // Empty state
  emptyMessage?: string;
  emptyIcon?: string;
  
  // Loading state
  loading?: boolean;
  loadingMessage?: string;
  
  // Role-based column visibility
  employeeRole?: string;
  employeeParticipant?: 'DBIS' | 'SCB' | 'FI';
  
  // Actions
  rowActions?: TableRowAction<T>[];
  bulkActions?: TableBulkAction<T>[];
}

export interface TableRowAction<T = unknown> {
  label: string;
  icon?: string;
  action: Action;
  module: string;
  onClick: (row: T) => void | Promise<void>;
  variant?: 'primary' | 'secondary' | 'danger';
  showIf?: (row: T) => boolean;
}

export interface TableBulkAction<T = unknown> {
  label: string;
  icon?: string;
  action: Action;
  module: string;
  onClick: (rows: T[]) => void | Promise<void>;
  variant?: 'primary' | 'secondary' | 'danger';
  minSelections?: number;
}

export const tableSizes = {
  sm: {
    padding: '0.375rem 0.75rem',
    fontSize: '0.875rem',
  },
  md: {
    padding: '0.5rem 1rem',
    fontSize: '1rem',
  },
  lg: {
    padding: '0.75rem 1.25rem',
    fontSize: '1.125rem',
  },
};

/**
 * Table Component Interface
 * 
 * Usage example (React):
 * ```tsx
 * <Table
 *   data={settlements}
 *   columns={columns}
 *   sortable
 *   filterable
 *   paginated
 *   pagination={{
 *     page: 1,
 *     pageSize: 20,
 *     total: 100
 *   }}
 *   rowActions={[
 *     {
 *       label: "View",
 *       action: "view",
 *       module: "settlement",
 *       onClick: (row) => viewSettlement(row.id)
 *     }
 *   ]}
 * />
 * ```
 */
export interface TableComponent<T = unknown> {
  props: TableProps<T>;
  sort(column: string, direction: SortDirection): void;
  filter(filters: TableFilter[]): void;
  selectRow(row: T): void;
  selectAll(): void;
  clearSelection(): void;
  render(): unknown;
}

