import KFIcon from '@/components/KFIcon';
import { CommonTabKeys } from '@/enums';
import {
  addDatasetVersion,
  addModelVersion,
  compareDatasetVersion,
  compareModelVersion,
  deleteDataset,
  deleteDatasetVersion,
  deleteModel,
  deleteModelVersion,
  getDatasetInfo,
  getDatasetList,
  getDatasetVersionList,
  getModelInfo,
  getModelList,
  getModelVersionList,
} from '@/services/dataset/index.js';
import { limitUploadFileType } from '@/utils/ui';
import type { TabsProps, UploadFile } from 'antd';

export enum ResourceType {
  Model = 'Model', // 模型
  Dataset = 'Dataset', // 数据集
}

export enum DataSource {
  AutoExport = 'auto_export', // 自动导出
  HandExport = 'hand_export', // 手动导出
  Create = 'add', // 新增
  LabelStudioExport = 'label_studio_export', // LabelStudio 导出
}

type ResourceTypeInfo = {
  getList: (params: any) => Promise<any>; // 获取资源列表
  getVersions: (params: any) => Promise<any>; // 获取版本列表
  deleteRecord: (params: any) => Promise<any>; // 删除
  addVersion: (params: any) => Promise<any>; // 新增版本
  deleteVersion: (params: any) => Promise<any>; // 删除版本
  getInfo: (params: any) => Promise<any>; // 获取详情
  compareVersion: (params: any) => Promise<any>; // 版本对比
  name: string; // 名称
  typeParamKey: 'data_type' | 'model_type'; // 类型参数名称，获取资源列表接口使用
  tagParamKey: 'data_tag' | 'model_tag'; // 标签参数名称，获取资源列表接口使用
  filePropKey: 'dataset_version_vos' | 'model_version_vos'; // 文件列表属性
  sourceParamKey: 'dataset_source' | 'model_source'; // 来源参数名称
  tabItems: TabsProps['items']; // tab 列表
  typeTitle: string; // 类型标题
  tagTitle: string; // 标签标题
  typeValue: number; // 从 getAssetIcon 接口获取特定值的数据为 type 分类 （category_id === typeValue）
  tagValue: number; // 从 getAssetIcon 接口获取特定值的数据为 tag 分类（category_id === tagValue）
  prefix: string; // 图片资源、详情 url 的前缀
  deleteModalTitle: string; // 删除弹框的title
  addBtnTitle: string; // 新增按钮的title
  uploadAction: string; // 上传接口 url
  uploadAccept?: string; // 上传文件类型
  beforeUpload?: (file: UploadFile) => boolean | string;
  downloadAllAction: string; // 批量下载接口 url
  downloadSingleAction: string; // 单个下载接口 url
};

export const resourceConfig: Record<ResourceType, ResourceTypeInfo> = {
  [ResourceType.Dataset]: {
    getList: getDatasetList,
    getVersions: getDatasetVersionList,
    deleteRecord: deleteDataset,
    addVersion: addDatasetVersion,
    deleteVersion: deleteDatasetVersion,
    getInfo: getDatasetInfo,
    compareVersion: compareDatasetVersion,
    name: '数据集',
    typeParamKey: 'data_type',
    tagParamKey: 'data_tag',
    filePropKey: 'dataset_version_vos',
    sourceParamKey: 'dataset_source',
    tabItems: [
      {
        key: CommonTabKeys.Public,
        label: '数据广场',
        icon: <KFIcon type="icon-shujuguangchang" />,
      },
      {
        key: CommonTabKeys.Private,
        label: '个人数据',
        icon: <KFIcon type="icon-gerenshuju" />,
      },
    ],
    typeTitle: '分类',
    tagTitle: '研究方向/应用领域',
    typeValue: 1,
    tagValue: 2,
    prefix: 'dataset',
    deleteModalTitle: '确定删除该条数据集实例吗？',
    addBtnTitle: '新建数据集',
    uploadAction: '/api/mmp/newdataset/upload',
    uploadAccept: '.zip,.tgz',
    beforeUpload: limitUploadFileType('zip,tgz'),
    downloadAllAction: '/api/mmp/newdataset/downloadAllFiles',
    downloadSingleAction: '/api/mmp/newdataset/downloadSingleFile',
  },
  [ResourceType.Model]: {
    getList: getModelList,
    getVersions: getModelVersionList,
    deleteRecord: deleteModel,
    addVersion: addModelVersion,
    deleteVersion: deleteModelVersion,
    getInfo: getModelInfo,
    compareVersion: compareModelVersion,
    name: '模型',
    typeParamKey: 'model_type',
    tagParamKey: 'model_tag',
    filePropKey: 'model_version_vos',
    sourceParamKey: 'model_source',
    tabItems: [
      {
        key: CommonTabKeys.Public,
        label: '模型广场',
        icon: <KFIcon type="icon-moxingguangchang" />,
      },
      {
        key: CommonTabKeys.Private,
        label: '个人模型',
        icon: <KFIcon type="icon-gerenmoxing" />,
      },
    ],
    typeTitle: '模型框架',
    tagTitle: '模型能力',
    typeValue: 3,
    tagValue: 4,
    prefix: 'model',
    deleteModalTitle: '确定删除该条模型实例吗？',
    addBtnTitle: '新建模型',
    uploadAction: '/api/mmp/newmodel/upload',
    uploadAccept: undefined,
    downloadAllAction: '/api/mmp/newmodel/downloadAllFiles',
    downloadSingleAction: '/api/mmp/newmodel/downloadSingleFile',
  },
};

// 分类数据
export type CategoryData = {
  id: number;
  category_id: number;
  name: string;
  path: string;
};

// 数据集、模型列表数据
export interface ResourceData {
  resourceType: ResourceType.Dataset | ResourceType.Model; // 用于 ts 类型判断
  id: number;
  name: string;
  identifier: string;
  owner: string;
  version: string;
  is_public: boolean;
  description?: string;
  create_by?: string;
  update_time?: string;
  time_ago?: string;
  version_desc?: string;
  usage?: string;
  relative_paths?: string;
  train_task?: TrainTask; // 训练任务
  praises_count: number; // 点赞数
  praised: boolean; // 是否点赞
  full_last_update_time: string; // 完整的更新时间
}

// 数据集数据
export interface DatasetData extends ResourceData {
  resourceType: ResourceType.Dataset; // 用于区别类型
  data_type?: string; // 数据集分类
  data_tag?: string; // 研究方向
  processing_code?: ProjectDependency; // 处理代码
  dataset_source?: string; // 数据来源
  dataset_version_vos?: ResourceFileData[];
}

// 模型数据
export interface ModelData extends ResourceData {
  resourceType: ResourceType.Model; // 用于区别类型
  model_type?: string; // 模型框架
  model_tag?: string; // 模型能力
  image?: string; // 训练镜像
  code?: string; // 训练镜像
  train_datasets?: DatasetData[]; // 训练数据集
  test_datasets?: DatasetData[]; // 测试数据集
  params?: Record<string, string>; // 参数
  metrics?: Record<string, string>; // 指标
  project_depency?: ProjectDependency; // 项目依赖
  model_source?: string; // 模型来源
  model_version_vos?: ResourceFileData[];
}

// 版本数据
export type ResourceVersionData = {
  name: string;
  http_url: string;
  tar_url: string;
  zip_url: string;
};

// 版本文件数据
export type ResourceFileData = {
  file_name: string;
  file_size: string;
  url: string;
  update_time?: string;
};

// 训练任务
export type TrainTask = {
  ins_id: number; // 实例id
  name: string; // 实验名称
  experiment_id: number; //实验 id
  workflow_id: number; // 流水线 id
};

// 项目依赖
export type ProjectDependency = {
  url: string; // 项目地址
  name: string; // 项目名称
  branch: string; // 分支
};
