import { DataType, TableType } from "@/enum";
import { ColumnItem, TableItemType } from "@/type";
import { uuid } from "@repo/utils";

/**
 * 获取token
 */
export const getToken = () => {
  const enterpriseCode = sessionStorage.getItem("enterpriseCode");
  const token = localStorage.getItem("token_" + enterpriseCode);

  return token;
};

export const createTable = (tableType: TableType, dataModelId: string, parentId?: string): TableItemType => {
  const tableId = uuid();
  const tableColumnList: ColumnItem[] = [];
  if(tableType === TableType.FlowTable) {
    // 创建流程表预定义字段
    const list = createFlowPredefinedFields(tableId, !!parentId);
    tableColumnList.push(...list);
  } else {
    // 创建业务表预定义字段
    tableColumnList.push(...createBasePredefinedField(tableId));
  }
  return {
    isTable: true,
    table: {
      aliasName: `newTableAliasName${new Date().getTime()}`,
      creationTime: "",
      creatorUserId: "",
      displayOrder: 0,
      id: tableId,
      isDeleted: false,
      langName: "",
      dataModelId,
      parentBusinessTableId: parentId || "",
      schemaName: `new_table_${new Date().getTime()}`,
      type: tableType,
      updateTime: "",
      openSync: false,
      langNameList: [
        { name: "zh-CN", value: "新建表" },
        { name: "en", value: "new table" },
      ],
      langDescriptionList: [
        { name: "zh-CN", value: "" },
        { name: "en", value: "" },
      ],
      style: {
        // 随机颜色
        color: "#" + Math.floor(Math.random() * 0x666666).toString(16),
      },
    },
    tableColumnList,
  };
};

/**
 * 创建字段
 * @param tableId 表id
 * @returns
 */
export const createColumn = (tableId?: string, displayOrder?: number): ColumnItem => {
  return {
    id: uuid(),
    schemaName: "",
    type: DataType.Nvarchar,
    maxLength: undefined,
    precision: undefined,
    scale: undefined,
    isRequired: false,
    isUnique: false,
    isPreDefined: false,
    defaultValue: "",
    displayOrder,
    tableId: tableId || "",
    memo: "",
    whereInputType: "",
    whereInputContent: "",
    charset: "",
    langNameList: [
      { name: "zh-CN", value: "" },
      { name: "en", value: "" },
    ],
  };
};

/**
 * 创建基础表预定义字段
 */
export const createBasePredefinedField = (tableId: string): ColumnItem[] => {
  return [
    // id
    {
      id: uuid(),
      schemaName: "id",
      type: DataType.Nvarchar,
      displayOrder: 1,
      maxLength: 50,
      tableId,
      isRequired: true,
      isUnique: false,
      isPreDefined: true,
      langNameList: [
        { name: "zh-CN", value: "Id" },
        { name: "en", value: "Id" },
      ],
    },
    // 创建时间
    {
      id: uuid(),
      schemaName: "creation_time",
      type: DataType.DateTime,
      displayOrder: 9001,
      maxLength: 0,
      tableId,
      isRequired: true,
      isUnique: false,
      isPreDefined: true,
      defaultValue: "<%SystemDate%>",
      langNameList: [
        { name: "zh-CN", value: "创建时间" },
        { name: "en", value: "CreationTime" },
      ],
    },
    // 创建者
    {
      id: uuid(),
      schemaName: "creation_user",
      type: DataType.Nvarchar,
      displayOrder: 9002,
      maxLength: 50,
      tableId,
      isRequired: true,
      isUnique: false,
      isPreDefined: true,
      defaultValue: "<%UserId%>",
      langNameList: [
        { name: "zh-CN", value: "创建人" },
        { name: "en", value: "CreationUser" },
      ],
    },
    // 更新时间
    {
      id: uuid(),
      schemaName: "update_time",
      type: DataType.DateTime,
      displayOrder: 9003,
      maxLength: 0,
      tableId,
      isRequired: false,
      isUnique: false,
      isPreDefined: true,
      defaultValue: "<%SystemDate%>",
      langNameList: [
        { name: "zh-CN", value: "更新时间" },
        { name: "en", value: "UpdateTime" },
      ],
    },
    // 更新者
    {
      id: uuid(),
      schemaName: "update_user",
      type: DataType.Nvarchar,
      displayOrder: 9004,
      maxLength: 50,
      tableId,
      isRequired: false,
      isUnique: false,
      isPreDefined: true,
      defaultValue: "<%UserId%>",
      langNameList: [
        { name: "zh-CN", value: "更新人" },
        { name: "en", value: "UpdateUser" },
      ],
    },
    // 删除时间
    {
      id: uuid(),
      schemaName: "delete_time",
      type: DataType.DateTime,
      displayOrder: 9005,
      maxLength: 0,
      tableId,
      isRequired: false,
      isUnique: false,
      isPreDefined: true,
      langNameList: [
        { name: "zh-CN", value: "删除时间" },
        { name: "en", value: "DeleteTime" },
      ],
    },
    // 删除人
    {
      id: uuid(),
      schemaName: "delete_user",
      type: DataType.Nvarchar,
      displayOrder: 9006,
      maxLength: 50,
      tableId,
      isRequired: false,
      isUnique: false,
      isPreDefined: true,
      defaultValue: "<%UserId%>",
      langNameList: [
        { name: "zh-CN", value: "删除人" },
        { name: "en", value: "DeleteUser" },
      ],
    },
    // 删除状态
    {
      id: uuid(),
      schemaName: "is_deleted",
      type: DataType.Bit,
      displayOrder: 9007,
      maxLength: 0,
      tableId,
      isRequired: false,
      isUnique: false,
      isPreDefined: true,
      langNameList: [
        { name: "zh-CN", value: "删除状态" },
        { name: "en", value: "IsDeleted" },
      ],
    },
  ];
};

/**
 * 创建流程表预定义字段
 */
export const createFlowPredefinedFields = (
  tableId: string,
  isMainTable: boolean
) => {
  return [
    // id
    {
      id: uuid(),
      schemaName: "id",
      type: DataType.Nvarchar,
      displayOrder: 1,
      maxLength: 50,
      tableId,
      isRequired: true,
      isUnique: false,
      isPreDefined: true,
      langNameList: [
        { name: "zh-CN", value: "Id" },
        { name: "en", value: "Id" },
      ],
    },
    // 任务id
    {
      id: uuid(),
      schemaName: "TaskId",
      type: DataType.Nvarchar,
      displayOrder: 2,
      maxLength: 50,
      tableId,
      isRequired: true,
      isUnique: false,
      isPreDefined: true,
      langNameList: [
        { name: "zh-CN", value: "任务Id" },
        { name: "en", value: "TaskId" },
      ],
    },
    ...(isMainTable
      ? [
          {
            id: uuid(),
            schemaName: "snNumber",
            type: DataType.Nvarchar,
            displayOrder: 3,
            maxLength: 50,
            tableId,
            isRequired: true,
            isUnique: false,
            isPreDefined: true,
            langNameList: [
              { name: "zh-CN", value: "单号" },
              { name: "en", value: "SnNumber" },
            ],
          },
          {
            id: uuid(),
            schemaName: "taskStatus",
            type: DataType.Int,
            displayOrder: 4,
            maxLength: 0,
            tableId,
            isRequired: false,
            isUnique: false,
            isPreDefined: true,
            langNameList: [
              { name: "zh-CN", value: "任务状态" },
              { name: "en", value: "TaskStatus" },
            ],
          },
          {
            id: uuid(),
            schemaName: "creationTime",
            type: DataType.DateTime,
            displayOrder: 5,
            tableId,
            isRequired: true,
            isUnique: false,
            isPreDefined: true,
            langNameList: [
              { name: "zh-CN", value: "创建时间" },
              { name: "en", value: "CreationTime" },
            ],
          },
        ]
      : [
          {
            id: uuid(),
            schemaName: "gridOrder",
            type: DataType.Int,
            displayOrder: 3,
            tableId,
            isRequired: false,
            isUnique: false,
            isPreDefined: true,
            langNameList: [
              { name: "zh-CN", value: "序号" },
              { name: "en", value: "GridOrder" },
            ],
          },
        ]),
  ];
};
