import React from "react";
import {
  CheckCircleOutlined,
  ClockCircleOutlined,
  CloseCircleOutlined,
  FolderOpenOutlined,
} from "@ant-design/icons";
import { BookOpen } from "lucide-react";
import { formatDateTime } from "@/utils/unit";
import {
  KnowledgeContentType,
  KnowledgeItem,
  KnowledgeSet,
  KnowledgeSourceType,
  KnowledgeStatusType,
} from "./knowledge-management.model";

export type KnowledgeStatusMeta = {
  label: string;
  value: KnowledgeStatusType;
  color: string;
  icon: React.ReactNode;
};

export const knowledgeStatusMap: Record<KnowledgeStatusType, KnowledgeStatusMeta> = {
  [KnowledgeStatusType.DRAFT]: {
    label: "草稿",
    value: KnowledgeStatusType.DRAFT,
    color: "#a1a1a1",
    icon: <ClockCircleOutlined />,
  },
  [KnowledgeStatusType.PUBLISHED]: {
    label: "已发布",
    value: KnowledgeStatusType.PUBLISHED,
    color: "#409f17",
    icon: <CheckCircleOutlined />,
  },
  [KnowledgeStatusType.ARCHIVED]: {
    label: "已归档",
    value: KnowledgeStatusType.ARCHIVED,
    color: "#6b7280",
    icon: <FolderOpenOutlined />,
  },
  [KnowledgeStatusType.DEPRECATED]: {
    label: "已废弃",
    value: KnowledgeStatusType.DEPRECATED,
    color: "#d97706",
    icon: <CloseCircleOutlined />,
  },
};

export const knowledgeStatusOptions = Object.values(knowledgeStatusMap).map((item) => ({
  label: item.label,
  value: item.value,
}));

export const knowledgeContentTypeOptions = [
  { label: "文本", value: KnowledgeContentType.TEXT },
  { label: "Markdown", value: KnowledgeContentType.MARKDOWN },
  { label: "文件", value: KnowledgeContentType.FILE },
];

export const knowledgeSourceTypeOptions = [
  { label: "手工录入", value: KnowledgeSourceType.MANUAL },
  { label: "数据集文件导入", value: KnowledgeSourceType.DATASET_FILE },
  { label: "文件上传", value: KnowledgeSourceType.FILE_UPLOAD },
];

// export const sensitivityOptions = [
//   { label: "敏感", value: "敏感" },
//   { label: "不敏感", value: "不敏感" },
// ];

export type KnowledgeSetView = {
  id: string;
  name: string;
  description?: string;
  status: KnowledgeStatusMeta | null;
  rawStatus: KnowledgeStatusType;
  tags: KnowledgeSet["tags"];
  domain?: string;
  businessLine?: string;
  owner?: string;
  validFrom?: string;
  validTo?: string;
  sourceType?: KnowledgeSet["sourceType"];
  sensitivity?: string;
  metadata?: string;
  createdAt?: string;
  updatedAt?: string;
  type: string;
  icon?: React.ReactNode;
  iconColor?: string;
  statistics?: { label: string; value: string | number }[];
};

export type KnowledgeItemView = {
  id: string;
  setId: string;
  content: string;
  contentType: KnowledgeContentType;
  status: KnowledgeStatusMeta | null;
  rawStatus: KnowledgeStatusType;
  tags: KnowledgeItem["tags"];
  domain?: string;
  businessLine?: string;
  owner?: string;
  validFrom?: string;
  validTo?: string;
  sourceType?: KnowledgeItem["sourceType"];
  sensitivity?: string;
  sourceDatasetId?: string;
  sourceFileId?: string;
  relativePath?: string;
  metadata?: string;
  createdAt?: string;
  updatedAt?: string;
};

export function mapKnowledgeSet(data: KnowledgeSet): KnowledgeSetView {
  return {
    id: data.id,
    name: data.name,
    description: data.description,
    status: knowledgeStatusMap[data.status] ?? null,
    rawStatus: data.status,
    tags: data.tags || [],
    domain: data.domain,
    businessLine: data.businessLine,
    owner: data.owner,
    validFrom: data.validFrom,
    validTo: data.validTo,
    sourceType: data.sourceType,
    sensitivity: data.sensitivity,
    metadata: data.metadata,
    createdAt: data.createdAt ? formatDateTime(data.createdAt) : "",
    updatedAt: data.updatedAt ? formatDateTime(data.updatedAt) : "",
    type: "知识集",
    icon: <BookOpen className="w-6 h-6" />,
    iconColor: "#3b82f6",
  };
}

export function mapKnowledgeItem(data: KnowledgeItem): KnowledgeItemView {
  return {
    id: data.id,
    setId: data.setId,
    content: data.content,
    contentType: data.contentType,
    status: knowledgeStatusMap[data.status] ?? null,
    rawStatus: data.status,
    tags: data.tags || [],
    domain: data.domain,
    businessLine: data.businessLine,
    owner: data.owner,
    validFrom: data.validFrom,
    validTo: data.validTo,
    sourceType: data.sourceType,
    sensitivity: data.sensitivity,
    sourceDatasetId: data.sourceDatasetId,
    sourceFileId: data.sourceFileId,
    relativePath: data.relativePath,
    metadata: data.metadata,
    createdAt: data.createdAt ? formatDateTime(data.createdAt) : "",
    updatedAt: data.updatedAt ? formatDateTime(data.updatedAt) : "",
  };
}
