// Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

import type { Props as CommonLinkProps } from '#shared/components/CommonLink/CommonLink.vue'
import type { EntityAttribute } from '#shared/entities/core/attributes/types/store.ts'
import type { EnumObjectManagerObjects, EnumOrderDirection } from '#shared/graphql/types.ts'

import type { MenuItem } from '#desktop/components/CommonPopoverMenu/types.ts'

import type { Awaitable } from '@vueuse/shared'

export const MINIMUM_COLUMN_WIDTH = 50
export const MINIMUM_TABLE_WIDTH = 600

type TableColumnType = 'timestamp' | 'timestamp_absolute' | 'link'

type TableHeaderPreference = {
  headerClass?: string
  labelClass?: string
  hideLabel?: boolean
  truncate?: boolean
  noResize?: boolean
  displayWidth?: number
  noSorting?: boolean
  minimumWidth?: number
}

type TableColumnPreference = {
  /**
   * @default 'left'
   * */
  alignContent?: 'center' | 'right' | 'left'
  noPadding?: boolean
}

export interface TableSimpleHeader<K = string>
  extends TableHeaderPreference, TableColumnPreference {
  key: K
  label: string
  labelPlaceholder?: string[]
  type?: TableColumnType
  columnSeparator?: boolean
  [key: string]: unknown
}

export type TableItemLinkValue = Partial<CommonLinkProps> & { label: string }

export interface TableItem {
  [key: string]: unknown | TableItemLinkValue
  id: ID | number
}

export interface TableAdvancedItem {
  [key: string]: unknown
  id: ID
  policy?: {
    update?: boolean
  }
}

export type TableRowClass = string | string[] | Record<string, boolean>

interface BaseTableProps {
  actions?: MenuItem[]
  caption: string
  showCaption?: boolean
}

export interface SimpleTableProps extends BaseTableProps {
  items: TableItem[]
  headers: TableSimpleHeader[]
  onClickRow?: (tableItem: TableItem) => void
  /**
   * Used to set a default selected row
   * Is not used for checkbox
   * */
  selectedRowId?: string
  hasCheckboxColumn?: boolean
}

export interface CellContentProps {
  value?: string | number
  isRowSelected?: boolean
}

export interface TableAttribute {
  name: string
  label: string
  labelPlaceholder?: string[]
  headerPreferences?: TableHeaderPreference
  columnPreferences?: TableColumnPreference & {
    link?: Pick<CommonLinkProps, 'internal' | 'openInNewTab'> & {
      getLink: (item: TableAdvancedItem, tableAttribute: TableAttribute) => string
    }
    tooltip?: (item: TableAdvancedItem) => string | undefined
  }
  dataType: EntityAttribute['dataType']
  dataOption?: EntityAttribute['dataOption']
}

export interface AdvancedTableProps extends BaseTableProps {
  items: TableAdvancedItem[]
  headers: string[]
  /**
   * Used to determine the total number of items for pagination and infinite scroll
   */
  totalItemsCount: number

  attributes?: TableAttribute[]
  attributeExtensions?: Record<string, Partial<TableAttribute>>
  /**
   * Do not offer sorting for columns which display the value of a related record.
   * Needed for the Elasticsearch-based search, which can only sort them by the raw
   * reference (e.g. `customer_id`) and not by the displayed value.
   */
  disableRelationSorting?: boolean
  entity?: EnumObjectManagerObjects
  /**
   * Used to set a default selected row
   * Is not used for checkbox
   * */
  selectedRowId?: string
  /**.
   * Indicates if the table has a checkbox column for bulk actions
   */
  hasBulkAction?: boolean

  /**
   * Disable all checkbox cells, requires hasBulkAction to be true
   */
  disableBulkAction?: boolean

  maxItems?: number

  onClickRow?: (tableItem: TableAdvancedItem) => void

  /**
   * Additional classes for a row, e.g. to highlight or dim it depending on the item
   */
  rowClass?: (tableItem: TableAdvancedItem) => TableRowClass

  reachedScrollTop?: boolean

  onLoadMore?: () => Awaitable<void>

  storageKeyId?: string

  scrollContainer?: HTMLElement | null

  groupBy?: string

  orderBy?: string
  orderDirection?: EnumOrderDirection

  isSorting?: boolean

  dynamicRowHeight?: boolean
}

export interface ListTableProps<T> {
  tableId: string
  headers: string[]
  /**
   * Do not offer sorting for columns which display the value of a related record.
   */
  disableRelationSorting?: boolean
  orderDirection?: EnumOrderDirection
  orderBy?: string
  groupBy?: string
  caption: string
  reachedScrollTop?: boolean
  scrollContainer?: HTMLElement | null
  items: T[]
  totalCount: number
  maxItems: number
  resorting?: boolean
  loading: boolean
  loadingNewPage: boolean
  skeletonLoadingCount?: number
  onLoadMore?: () => Awaitable<void>
}

export interface ListTableEmits {
  sort: [string, EnumOrderDirection]
}
