import type { VxeGridProps } from '#/adapter/vxe-table';

import { getAiGenerationList } from '../api';
import { normalizeAiGenerationFilters } from './search';

interface RowType {
  id: number;
  scene: string;
  name: string;
  status: number;
  provider: string;
  model: string;
  api_key_name: string;
  prompt_tokens: number;
  completion_tokens: number;
  total_tokens: number;
  duration_ms: number;
  error_msg: string;
  created_at: string;
  /** P4：采纳状态（0未采纳/1已采纳）与文案 */
  is_adopted: number;
  is_modified_final: number;
  adopted_txt: string;
}

/**
 * AI 生成记录表格
 */
export const gridOptions: VxeGridProps<RowType> = {
  checkboxConfig: {
    highlight: false,
  },
  columnConfig: {
    useKey: true,
  },
  rowConfig: {
    useKey: true,
  },
  columns: [
    { field: 'id', align: 'left', title: 'ID', width: 80 },
    { field: 'scene_txt', align: 'left', title: '场景', width: 90 },
    { field: 'name', align: 'left', title: '名称', minWidth: 140 },
    {
      field: 'status',
      align: 'left',
      title: '状态',
      width: 90,
      slots: { default: 'status' },
    },
    { field: 'provider', align: 'left', title: '供应商', width: 100 },
    { field: 'model', align: 'left', title: '模型', minWidth: 120 },
    { field: 'api_key_name', align: 'left', title: '密钥', minWidth: 110 },
    {
      field: 'total_tokens',
      align: 'right',
      title: 'Tokens',
      width: 110,
      slots: { default: 'tokens' },
    },
    {
      field: 'duration_ms',
      align: 'right',
      title: '耗时',
      width: 90,
      slots: { default: 'duration' },
    },
    {
      // P4：医生采纳情况（未采纳/直接采纳/改后采纳），直接采纳是质量最好的信号
      field: 'adopted_txt',
      align: 'left',
      title: '采纳',
      width: 96,
      slots: { default: 'adopted' },
    },
    {
      field: 'error_msg',
      align: 'left',
      title: '错误',
      minWidth: 160,
      showOverflow: true,
    },
    { field: 'created_at', align: 'left', title: '创建时间', width: 170 },
    {
      type: 'html',
      title: '操作',
      slots: { default: 'action' },
      width: 100,
      fixed: 'right',
    },
  ],
  keepSource: true,
  pagerConfig: {},
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues) => {
        const params = {
          page: page.currentPage,
          pageSize: page.pageSize,
          ...normalizeAiGenerationFilters(formValues || {}),
        };
        return await getAiGenerationList(params);
      },
    },
  },
  height: 'auto',
  border: false,
  toolbarConfig: {
    search: true,
    refresh: true,
    print: false,
    export: false,
    zoom: true,
    custom: {
      icon: 'vxe-icon-menu',
    },
  },
  showOverflow: true,
};
