import type { ReactNode } from 'react'
import type { Table } from '@tanstack/react-table'

import { cn } from '@evobgp/ui/lib/utils'

import { DataGridToolbar } from '@/components/data-grid-toolbar'
import {
  panelCardContentFlushClassName,
  panelCardFooterClassName,
} from '@/components/panel-card'
import { FrameDataGrid } from '@/components/reui-kit/frame-data-grid'
import { DataGridPagination } from '@/components/reui/data-grid/data-grid-pagination'
import { DataGrid, DataGridContainer } from '@/components/reui/data-grid/data-grid'
import { DataGridTable } from '@/components/reui/data-grid/data-grid-table'
import { FrameFooter } from '@/components/reui/frame'
import {
  DATA_GRID_MESSAGES_RU,
  DATA_GRID_PAGINATION_RU,
  DATA_GRID_TABLE_CLASS_NAMES,
  DATA_GRID_TABLE_LAYOUT,
} from '@/lib/data-grid-defaults'

interface DataGridShellProps<TData extends object> {
  table: Table<TData>
  recordCount: number
  isLoading?: boolean
  emptyMessage?: ReactNode
  showPagination?: boolean
  tableLayout?: typeof DATA_GRID_TABLE_LAYOUT
  className?: string
  onRowClick?: (row: TData) => void
}

export function DataGridShell<TData extends object>({
  table,
  recordCount,
  isLoading = false,
  emptyMessage,
  showPagination = true,
  tableLayout = DATA_GRID_TABLE_LAYOUT,
  className,
  onRowClick,
}: DataGridShellProps<TData>) {
  return (
    <DataGrid
      table={table}
      recordCount={recordCount}
      isLoading={isLoading}
      emptyMessage={emptyMessage ?? DATA_GRID_MESSAGES_RU.emptyMessage}
      loadingMessage={DATA_GRID_MESSAGES_RU.loadingMessage}
      tableLayout={tableLayout}
      tableClassNames={DATA_GRID_TABLE_CLASS_NAMES}
      className={className}
      onRowClick={onRowClick}
    >
      <DataGridContainer border={false}>
        <DataGridTable />
      </DataGridContainer>
      {showPagination ? (
        <FrameFooter className={cn(panelCardFooterClassName)}>
          <DataGridPagination {...DATA_GRID_PAGINATION_RU} />
        </FrameFooter>
      ) : null}
    </DataGrid>
  )
}

/** @deprecated Prefer FrameDataGrid from @/components/reui-kit */
export function DataGridCard({
  title,
  description,
  actions,
  children,
  className,
}: {
  title?: ReactNode
  description?: ReactNode
  actions?: ReactNode
  children: ReactNode
  className?: string
}) {
  return (
    <FrameDataGrid
      title={title}
      description={description}
      actions={actions}
      className={className}
    >
      <div className={panelCardContentFlushClassName}>{children}</div>
    </FrameDataGrid>
  )
}

interface DataGridSectionProps<TData extends object> extends DataGridShellProps<TData> {
  searchValue: string
  onSearchChange: (value: string) => void
  searchPlaceholder?: string
  toolbarFilters?: ReactNode
  toolbarActions?: ReactNode
  beforeGrid?: ReactNode
}

export function DataGridSection<TData extends object>({
  searchValue,
  onSearchChange,
  searchPlaceholder,
  toolbarFilters,
  toolbarActions,
  beforeGrid,
  ...shellProps
}: DataGridSectionProps<TData>) {
  return (
    <>
      <DataGridToolbar
        searchValue={searchValue}
        onSearchChange={onSearchChange}
        searchPlaceholder={searchPlaceholder}
        filters={toolbarFilters}
        actions={toolbarActions}
      />
      {beforeGrid}
      <DataGridShell {...shellProps} />
    </>
  )
}
