import React from 'react'

import { getValueFormat } from '@baurine/grafana-value-formats'
import { Typography, Tooltip } from '@tidbcloud/uikit'

import InfoTable from '../../components/InfoTable'
import { StatementModel } from '../utils/type'

export const PlanDetailTabCopr: React.FC<
  React.PropsWithChildren<{ data: StatementModel }>
> = ({ data }) => {
  return (
    <InfoTable<{
      name: string
      value: React.ReactNode
      description?: string
    }>
      columns={[
        {
          key: 'name',
          name: 'Name',
          element: ({ value }) => (
            <Typography className='wd-truncate'>{value}</Typography>
          )
        },
        {
          key: 'value',
          name: 'Value',
          element: ({ value }) => (
            <Tooltip label={value} position='top-start' withArrow>
              <Typography className='wd-truncate'>{value}</Typography>
            </Tooltip>
          ),
          tdStyle: { maxWidth: '300px' }
        },
        {
          key: 'description',
          name: 'Description',
          tdStyle: { maxWidth: '400px' },
          element: ({ value }) => (
            <Tooltip
              label={value}
              position='top-start'
              withArrow
              maw={300}
              multiline
            >
              <Typography className='wd-truncate'>{value}</Typography>
            </Tooltip>
          )
        }
      ]}
      data={[
        {
          name: 'Total Coprocessor Tasks',
          value: data.sum_cop_task_num ?? 0,
          description: ''
        },
        {
          name: 'Mean Visible Versions Per Query',
          value: getValueFormat('short')(data.avg_processed_keys ?? 0, 0, 1),
          description: ''
        },
        {
          name: 'Max Visible Versions Per Query',
          value: getValueFormat('short')(data.max_processed_keys ?? 0, 0, 1),
          description: ''
        },
        {
          name: 'Mean Meet Versions Per Query',
          value: getValueFormat('short')(data.avg_total_keys ?? 0, 0, 1),
          description: 'Meet versions contains overwritten or deleted versions'
        },
        {
          name: 'Max Meet Versions Per Query',
          value: getValueFormat('short')(data.max_total_keys ?? 0, 0, 1),
          description: ''
        },
        {
          name: 'Mean RocksDB Block Cache Hits',
          value: getValueFormat('short')(
            data.avg_rocksdb_block_cache_hit_count ?? 0,
            0,
            1
          ),
          description:
            'Total number of hits from the block cache (RocksDB block_cache_hit_count)'
        },
        {
          name: 'Max RocksDB Block Cache Hits',
          value: getValueFormat('short')(
            data.max_rocksdb_block_cache_hit_count ?? 0,
            0,
            1
          ),
          description: ''
        },
        {
          name: 'Mean RocksDB FS Read Size',
          value: getValueFormat('short')(
            data.avg_rocksdb_block_read_byte ?? 0,
            0,
            1
          ),
          description:
            'Total number of bytes RocksDB read from file (RocksDB block_read_byte)'
        },
        {
          name: 'Max RocksDB FS Read Size',
          value: getValueFormat('short')(
            data.max_rocksdb_block_read_byte ?? 0,
            0,
            1
          ),
          description: ''
        },
        {
          name: 'Mean RocksDB Block Reads',
          value: getValueFormat('short')(
            data.avg_rocksdb_block_read_count ?? 0,
            0,
            1
          ),
          description:
            'Total number of blocks RocksDB read from file (RocksDB block_read_count)'
        },
        {
          name: 'Max RocksDB Block Reads',
          value: getValueFormat('short')(
            data.max_rocksdb_block_read_count ?? 0,
            0,
            1
          ),
          description: ''
        },
        {
          name: 'Mean RocksDB Skipped Deletions',
          value: getValueFormat('short')(
            data.avg_rocksdb_delete_skipped_count ?? 0,
            0,
            1
          ),
          description:
            'Total number of deleted (a.k.a. tombstone) key versions that are skipped during iteration (RocksDB delete_skipped_count)'
        },
        {
          name: 'Max RocksDB Skipped Deletions',
          value: getValueFormat('short')(
            data.max_rocksdb_delete_skipped_count ?? 0,
            0,
            1
          ),
          description: ''
        },
        {
          name: 'Mean RocksDB Skipped Keys',
          value: getValueFormat('short')(
            data.avg_rocksdb_key_skipped_count ?? 0,
            0,
            1
          ),
          description:
            'Total number of keys skipped during iteration (RocksDB key_skipped_count)'
        },
        {
          name: 'Max RocksDB Skipped Keys',
          value: getValueFormat('short')(
            data.max_rocksdb_key_skipped_count ?? 0,
            0,
            1
          ),
          description: ''
        }
      ]}
    />
  )
}
