import { Button, Space, Tag } from 'antd';
import type { ProColumns } from '@ant-design/pro-components';
import dayjs from 'dayjs';
import type { AuditLogResultStatus, AuditLogVO } from './types';

/** 业务模块（后端可能扩展，未命中的直接展示原值）。 */
export const moduleText: Record<string, string> = {
  EMPLOYEE: '员工',
  ORG: '组织',
  ROLE: '角色',
  CUSTOMER: '客户',
  FOLLOW_UP: '跟进',
  TENANT: '租户',
  SYSTEM: '系统',
};

/** 操作动作（后端可能扩展，未命中的直接展示原值）。 */
export const actionText: Record<string, string> = {
  CREATE: '新增',
  UPDATE: '修改',
  DELETE: '删除',
  IMPORT: '导入',
  EXPORT: '导出',
  LOGIN: '登录',
  LOGOUT: '退出',
};

export const resultStatusMeta: Record<AuditLogResultStatus, { text: string; color: string }> = {
  SUCCESS: { text: '成功', color: 'success' },
  FAILURE: { text: '失败', color: 'error' },
};

const toValueEnum = (meta: Record<string, string>) =>
  Object.fromEntries(Object.entries(meta).map(([value, text]) => [value, { text }]));

export const getModuleText = (value?: string) => (value ? moduleText[value] ?? value : '-');

export const getActionText = (value?: string) => (value ? actionText[value] ?? value : '-');

export const createAuditLogColumns = (options: {
  onDetail: (record: AuditLogVO) => void;
}): ProColumns<AuditLogVO>[] => [
  {
    title: '日志 ID',
    dataIndex: 'logId',
    width: 100,
    search: false,
  },
  {
    title: '操作时间',
    dataIndex: 'createdAt',
    valueType: 'dateTime',
    width: 170,
    search: false,
  },
  {
    title: '操作人',
    dataIndex: 'operatorName',
    width: 140,
    search: false,
    render: (_value, record) => (
      <Space direction="vertical" size={0}>
        <span>{record.operatorName || '-'}</span>
        {record.operatorRole && <span className="text-muted">{record.operatorRole}</span>}
      </Space>
    ),
  },
  {
    title: '操作人 ID',
    dataIndex: 'operatorId',
    valueType: 'digit',
    hideInTable: true,
    fieldProps: { placeholder: '请输入操作人 ID' },
  },
  {
    title: '业务模块',
    dataIndex: 'module',
    width: 100,
    valueEnum: toValueEnum(moduleText),
    render: (_value, record) => getModuleText(record.module),
  },
  {
    title: '操作动作',
    dataIndex: 'action',
    width: 100,
    valueEnum: toValueEnum(actionText),
    render: (_value, record) => getActionText(record.action),
  },
  {
    title: '操作描述',
    dataIndex: 'operationDesc',
    width: 120,
    search: false,
    ellipsis: true,
    render: (_value, record) => record.operationDesc || '-',
  },
  {
    title: '操作对象',
    dataIndex: 'entityName',
    width: 180,
    search: false,
    ellipsis: true,
    render: (_value, record) => record.entityName || '-',
  },
  {
    title: '实体类型',
    dataIndex: 'entityType',
    hideInTable: true,
    fieldProps: { placeholder: '请输入实体类型' },
  },
  {
    title: '请求',
    dataIndex: 'requestPath',
    width: 220,
    search: false,
    ellipsis: true,
    render: (_value, record) => (
      <Space size={4}>
        <Tag bordered={false}>{record.requestMethod || '-'}</Tag>
        <span>{record.requestPath || '-'}</span>
      </Space>
    ),
  },
  {
    title: '来源 IP',
    dataIndex: 'requestIp',
    width: 130,
    search: false,
  },
  {
    title: '结果',
    dataIndex: 'resultStatus',
    width: 90,
    valueEnum: {
      SUCCESS: { text: resultStatusMeta.SUCCESS.text },
      FAILURE: { text: resultStatusMeta.FAILURE.text },
    },
    render: (_value, record) => {
      const meta = record.resultStatus ? resultStatusMeta[record.resultStatus] : undefined;
      return meta ? (
        <Tag color={meta.color} bordered={false}>
          {meta.text}
        </Tag>
      ) : (
        '-'
      );
    },
  },
  {
    title: '操作',
    key: 'option',
    valueType: 'option',
    width: 80,
    render: (_value, record) => (
      <Button type="link" size="small" onClick={() => options.onDetail(record)}>
        详情
      </Button>
    ),
  },
  {
    title: '操作时间',
    dataIndex: 'timeRange',
    valueType: 'dateRange',
    hideInTable: true,
    search: {
      transform: (value: [string, string]) => ({
        start: value?.[0] ? dayjs(value[0]).format('YYYY-MM-DD 00:00:00') : undefined,
        end: value?.[1] ? dayjs(value[1]).format('YYYY-MM-DD 23:59:59') : undefined,
      }),
    },
  },
];
