import type { UserInfo } from '@vben/types';

import { requestClient } from '#/api/request';

export namespace UserApi {
  export interface ApplicationFilter {
    name: string;
    value: boolean | number | string;
  }

  export interface GetMyApplicationListParams {
    currentPage: number;
    pageSize: number;
    orderByProperty?: string;
    Ascending?: boolean;
    totalPage?: number;
    totalCount?: number;
    filters?: ApplicationFilter[];
  }

  export interface GetMenuPermissionButtonsParams {
    menu_code: string;
  }

  export interface GetMenuPermissionButtonsResult {
    isSuccess: boolean;
    code: number;
    result: string[];
    isAuthorized: boolean;
  }

  export interface ApplicationModel {
    cellPhone: string;
    client_code: string;
    client_name: string;
    code: string;
    contact: string;
    from_code: string;
    from_name: string;
    id: string;
    imgLogoFileId: string;
    isEnable: boolean;
    isOwner: boolean;
    isReleased: boolean;
    langName: string;
    language: string;
    name: string;
    memo?: string;
    number0fBusinessScenarios: number;
    number0fCSiteMaxUser: number;
    number0fDesigners: number;
    number0fPages: number;
    number0fTables: number;
    number0fWorkFlow: number;
    usedBusinessScenarios: number;
    usedCSiteMaxUser: number;
    usedDesigners: number;
    usedPages: number;
    usedTables: number;
    usedWorkFlow: number;
    webSite: string;
    web_build_status: number;
  }

  export interface GetMyApplicationListResult {
    result: any;
    currentPage: number;
    hasNextPage: boolean;
    hasPreviousPage: boolean;
    model: ApplicationModel[];
    pageSize: number;
    totalCount: number;
    totalPages: number;
  }

  export interface DeliveryPartnerFilter {
    name: string;
    value?: boolean | number | string;
  }

  export interface GetDeliveryPartnersParams {
    currentPage: number;
    pageSize: number;
    orderByProperty?: string;
    Ascending?: boolean;
    totalPage?: number;
    totalCount?: number;
    filters?: DeliveryPartnerFilter[];
  }

  export interface DeliveryPartnerModel {
    account: string;
    id: string;
    langName: string;
    name: string;
    expiredTime?: string;
    number0fBusinessScenarios: number;
    number0fCSiteMaxUser: number;
    number0fDesigners: number;
    number0fEnterprise: number;
    number0fPages: number;
    number0fTables: number;
    number0fWorkFlow: number;
    status: boolean;
    usedBusinessScenarios: number;
    usedCSiteMaxUser: number;
    usedDesigners: number;
    usedEnterprise: number;
    usedPages: number;
    usedTables: number;
    usedWorkFlow: number;
  }

  export interface UserModel {
    accessId: string;
    account: string;
    cellPhone: string;
    chineseName: string;
    englishName: string;
    id: string;
    isActive: boolean;
    isSuperAdmin: boolean;
    langName: string;
    language: string;
    name: string;
    expiredTime?: string;
    emailAddress?: string;
  }

  export interface LangName {
    name: string;
    value: string;
  }

  export interface AddApplicationParams {
    isEnable: boolean;
    langNameList: LangName[];
    fileId?: string;
    imgLogoFileId?: string;
    version?: string;
    partnerInfoId?: string;
    code?: string;
    from_code?: string;
    number0fWorkFlow?: number;
    usedWorkFlow?: number;
    usedPages?: number;
    number0fPages?: number;
    usedTables?: number;
    number0fTables?: number;
    usedDesigners?: number;
    number0fDesigners?: number;
    number0fBusinessScenarios?: number;
    usedBusinessScenarios?: number;
    usedCSiteMaxUser?: number;
    number0fCSiteMaxUser?: number;
    langName?: null | string;
    contact?: string;
    phone?: string;
    email?: string;
    address?: string;
    description?: string;
    customerRelation?: string;
    nameCn?: string;
    nameEn?: string;
    cloneProject?: string;
    projectCode?: string;
    partner?: string;
  }

  export interface AddApplicationResult {
    isSuccess: boolean;
    code: number;
    result: boolean;
    isAuthorized: boolean;
  }

  export interface GenerateSystemSecretParams {
    begin_date: any;
    encrypt_type: number;
    enterprise_code: string;
    error_msg: string;
    is_permanent: boolean;
    period_day: number;
  }

  export interface GenerateSystemSecretResult {
    isSuccess: boolean;
    code: number;
    result: any;
    isAuthorized: boolean;
  }

  export interface ApplicationDetailResult {
    isSuccess: boolean;
    code: number;
    result: any;
    isAuthorized: boolean;
  }

  export interface AssociatedUser {
    id: string;
    account: string;
    role: string;
  }

  export interface GetAssociatedUsersResult {
    isSuccess: boolean;
    code: number;
    result: AssociatedUser[];
    isAuthorized: boolean;
  }

  export interface AssociateUserParams {
    enterpriseId: string;
    userId: string;
    roleTypes: string;
  }

  export interface AssociateUserResult {
    isSuccess: boolean;
    code: number;
    result: boolean;
    isAuthorized: boolean;
  }

  export interface GetSystemSecretResult {
    isSuccess: boolean;
    code: number;
    result: {
      encryptInfo: string;
      generateSystemEncryptInfo: {
        begin_date: string;
        error_msg: string;
        is_permanent: boolean;
        period_day: number;
        system_key: string;
      };
    };
    isAuthorized: boolean;
  }

  export interface GetDeliveryPartnersResult {
    result: any;
    currentPage: number;
    hasNextPage: boolean;
    hasPreviousPage: boolean;
    model: UserModel[];
    pageSize: number;
    totalCount: number;
    totalPages: number;
  }

  export interface PartnerFilter {
    name: string;
    value?: boolean | number | string;
  }

  export interface GetPartnersListParams {
    currentPage: number;
    pageSize: number;
    orderByProperty?: string;
    Ascending?: boolean;
    totalPage?: number;
    totalCount?: number;
    filters?: PartnerFilter[];
  }

  export interface PartnerModel {
    account: string;
    id: string;
    imgLogoFileId: string;
    langName: string;
    name: string;
    expiredTime?: string;
    number0fBusinessScenarios: number;
    number0fCSiteMaxUser: number;
    number0fDesigners: number;
    number0fEnterprise: number;
    number0fPages: number;
    number0fTables: number;
    number0fWorkFlow: number;
    status: boolean;
    usedBusinessScenarios: number;
    usedCSiteMaxUser: number;
    usedDesigners: number;
    usedEnterprise: number;
    usedPages: number;
    usedTables: number;
    usedWorkFlow: number;
  }

  export interface GetPartnersListResult {
    isSuccess: boolean;
    code: number;
    result: {
      currentPage: number;
      hasNextPage: boolean;
      hasPreviousPage: boolean;
      model: PartnerModel[];
      pageSize: number;
      totalCount: number;
      totalPages: number;
    };
    isAuthorized: boolean;
  }

  export interface DeletePartnerResult {
    isSuccess: boolean;
    code: number;
    result: boolean;
    isAuthorized: boolean;
  }

  export interface CreatePartnerParams {
    langNameList: Array<{
      name: string;
      value: string;
    }>;
    fileId?: string;
    imgLogoFileId?: string;
    version?: string;
    account?: string;
    langName?: string;
    password?: string;
    expiredTime?: string;
    number0fEnterprise?: number;
    number0fPages?: number;
    number0fDesigners?: number;
    number0fCSiteMaxUser?: number;
    number0fBusinessScenarios?: number;
    number0fTables?: number;
    number0fWorkFlow?: number;
    status?: boolean;
  }

  export interface CreatePartnerResult {
    isSuccess: boolean;
    code: number;
    result: boolean;
    isAuthorized: boolean;
  }

  export interface UpdatePartnerParams {
    id?: string;
    langNameList: Array<{
      name: string;
      value: string;
    }>;
    fileId?: string;
    imgLogoFileId?: string;
    version?: string;
    account?: string;
    langName?: string;
    password?: string;
    expiredTime?: string;
    number0fEnterprise?: number;
    number0fPages?: number;
    number0fDesigners?: number;
    number0fCSiteMaxUser?: number;
    number0fBusinessScenarios?: number;
    number0fTables?: number;
    number0fWorkFlow?: number;
    status?: boolean;
  }

  export interface UpdatePartnerResult {
    isSuccess: boolean;
    code: number;
    result: boolean;
    isAuthorized: boolean;
  }

  export interface GetLangByKeyResult {
    isSuccess: boolean;
    code: number;
    result: {
      en: string;
      key: string;
      'zh-CN': string;
    };
    isAuthorized: boolean;
  }

  export interface AddUserParams {
    langNameList: Array<{
      name: string;
      value: string;
    }>;
    expiredTime: string;
    langName: null | string;
    account: string;
    password: string;
    cellPhone: string;
    emailAddress: string;
    gogs_email: string;
    isActive?: boolean;
  }

  export interface UpdateUserParams {
    id: string;
    langNameList: Array<{
      name: string;
      value: string;
    }>;
    expiredTime: string;
    langName: null | string;
    account: string;
    password: string;
    cellPhone: string;
    emailAddress: string;
    gogs_email: string;
    isActive?: boolean;
  }

  export interface UserResult {
    isSuccess: boolean;
    code: number;
    result: boolean;
    isAuthorized: boolean;
  }

  export interface ProjectModel {
    id: string;
    name: string;
    code: string;
  }

  export interface GetAvailableProjectsResult {
    isSuccess: boolean;
    code: number;
    result: {
      model: ProjectModel[];
    };
    isAuthorized: boolean;
  }

  export interface EnterpriseCustomerFilter {
    name: string;
    value?: boolean | number | string;
  }

  export interface GetEnterpriseCustomersParams {
    currentPage: number;
    pageSize: number;
    orderByProperty?: string;
    Ascending?: boolean;
    totalPage?: number;
    totalCount?: number;
    filters?: EnterpriseCustomerFilter[];
  }

  export interface EnterpriseCustomerModel {
    code: string;
    id: string;
    imgLogoFileId: string;
    name: string;
    partnerUserName: string;
    contact?: string;
    cellPhone?: string;
    email?: string;
    address?: string;
    website?: string;
    remark?: string;
  }

  export interface GetEnterpriseCustomersResult {
    isSuccess: boolean;
    code: number;
    result: {
      currentPage: number;
      hasNextPage: boolean;
      hasPreviousPage: boolean;
      model: EnterpriseCustomerModel[];
      pageSize: number;
      totalCount: number;
      totalPages: number;
    };
    isAuthorized: boolean;
  }

  export interface EnterpriseCustomerParams {
    id?: string;
    fileId?: string;
    imgLogoFileId?: string;
    version?: string;
    code: string;
    name: string;
    contact?: string;
    cellPhone?: string;
    email?: string;
    address?: string;
    website?: string;
    remark?: string;
  }

  export interface EnterpriseCustomerResult {
    isSuccess: boolean;
    code: number;
    result: boolean;
    isAuthorized: boolean;
  }
}

/**
 * 获取用户信息
 */
export async function fetchUserInfoApi() {
  return requestClient.get<UserInfo>('/user/info');
}

/**
 * 获取我的应用列表
 */
export async function getMyApplicationListApi(
  data: UserApi.GetMyApplicationListParams,
) {
  return requestClient.post<UserApi.GetMyApplicationListResult>(
    '/api/enterprise/myList',
    data,
  );
}

/**
 * 获取交付伙伴
 */
export async function getDeliveryPartnersApi(
  data: UserApi.GetDeliveryPartnersParams,
) {
  return requestClient.post<UserApi.GetDeliveryPartnersResult>(
    '/api/user/list',
    data,
  );
}

/**
 * 重置密码
 */
export async function resetPasswordApi(data: {
  newPassword: string;
  userId: string;
}) {
  return requestClient.post('/api/account/doResetPassword', data);
}

/**
 * 获取合作伙伴
 */
export async function getPartnersApi() {
  return requestClient.post('/api/partner/AllList');
}

/**
 * 获取合作伙伴列表
 */
export async function getPartnersListApi(data: UserApi.GetPartnersListParams) {
  return requestClient.post<UserApi.GetPartnersListResult>(
    '/api/partner/list',
    data,
  );
}

/**
 * 创建合作伙伴
 */
export async function createPartnerApi(data: UserApi.CreatePartnerParams) {
  return requestClient.post<UserApi.CreatePartnerResult>(
    '/api/partner/doCreate',
    data,
  );
}

/**
 * 更新合作伙伴
 */
export async function updatePartnerApi(data: UserApi.UpdatePartnerParams) {
  return requestClient.post<UserApi.UpdatePartnerResult>(
    '/api/partner/doUpdate',
    data,
  );
}

/**
 * 获取语言详情
 */
export async function getLangByKeyApi(key: string) {
  return requestClient.post<UserApi.GetLangByKeyResult>(
    '/api/system/ListLangByKey',
    { key },
  );
}

/**
 * 删除合作伙伴
 */
export async function deletePartnerApi(data: { id: string }) {
  return requestClient.post<UserApi.DeletePartnerResult>(
    '/api/partner/doDelete',
    data,
  );
}

/**
 * 获取可以克隆的项目
 */
export async function getCloneApplicationsApi(
  data: UserApi.GetDeliveryPartnersParams,
) {
  return requestClient.post('/api/enterprise/myList', data);
}

/**
 * 获取客户关系
 */
export async function getCustomerRelationsApi() {
  return requestClient.post('/api/enterclient/alllist');
}

/**
 * 添加应用
 */
export async function addApplicationApi(data: UserApi.AddApplicationParams) {
  return requestClient.post<UserApi.AddApplicationResult>(
    '/api/enterprise/doCreate',
    data,
  );
}

/**
 * 更新应用
 */
export async function updateApplicationApi(data: UserApi.AddApplicationParams) {
  return requestClient.post<UserApi.AddApplicationResult>(
    '/api/enterprise/doUpdate',
    data,
  );
}

/**
 * 获取应用详情
 */
export async function getApplicationDetailApi(code: string) {
  return requestClient.post<UserApi.ApplicationDetailResult>(
    '/api/enterprise/info',
    { code },
  );
}

/**
 * 获取关联用户
 */
export async function getAssociatedUsersApi(enterprise_code: string) {
  return requestClient.post<UserApi.GetAssociatedUsersResult>(
    '/api/enterprise/listEnterpriseUser',
    { enterprise_code },
  );
}

/**
 * 获取可添加用户
 */
export async function getAddUsersApi(data: UserApi.GetMyApplicationListParams) {
  return requestClient.post<UserApi.GetMyApplicationListResult>(
    '/api/bpm/GetAllEnterpriseUser',
    data,
  );
}

/**
 * 关联用户保存
 */
export async function associateUserApi(data: UserApi.AssociateUserParams) {
  return requestClient.post<UserApi.AssociateUserResult>(
    '/api/enterprise/updateEnterpriseUser',
    data,
  );
}

/**
 * 给应用添加用户
 */
export async function addUserToApplicationApi(
  account: string,
  enterprise_code: string,
) {
  return requestClient.post<UserApi.AssociateUserResult>(
    '/api/enterprise/tryAddEnterpriseUser',
    { account, enterprise_code },
  );
}

/**
 * 删除应用用户
 */
export async function deleteUserFromApplicationApi(
  userId: string,
  enterpriseIdList: string[],
) {
  return requestClient.post<UserApi.AssociateUserResult>(
    '/api/user/doDeleteUserEnterprise',
    { userId, enterpriseIdList },
  );
}

/**
 * 获取系统密钥
 */
export async function getSystemSecretApi(enterprise_code: string) {
  return requestClient.post<UserApi.GetSystemSecretResult>(
    '/api/enterprise/getSystemEncryptInfo',
    { enterprise_code },
  );
}

/**
 * 生成系统密钥
 */
export async function generateSystemSecretApi(
  data: UserApi.GenerateSystemSecretParams,
) {
  return requestClient.post<UserApi.GenerateSystemSecretResult>(
    '/api/enterprise/doGenerateSystemEncryptInfo',
    data,
  );
}

/**
 * 删除应用
 */
export async function deleteApplicationApi(data: { id: string }) {
  return requestClient.post<UserApi.AddApplicationResult>(
    '/api/enterprise/doDelete',
    data,
  );
}

/**
 * 添加用户
 */
export async function addUserApi(data: UserApi.AddUserParams) {
  return requestClient.post<UserApi.UserResult>('/api/user/doCreate', data);
}

/**
 * 更新用户
 */
export async function updateUserApi(data: UserApi.UpdateUserParams) {
  return requestClient.post<UserApi.UserResult>(
    '/api/user/doUpdateWithAdmin',
    data,
  );
}

/**
 * 删除用户
 */
export async function deleteUserApi(data: any) {
  return requestClient.post<UserApi.UserResult>('/api/user/doDelete', data);
}

/**
 * 获取用户关联项目
 */
export async function getAssociatedProjectsApi(userId: string) {
  return requestClient.post<UserApi.GetAvailableProjectsResult>(
    '/api/user/listUserEnterprise',
    {
      filters: [
        {
          name: 'userId',
          value: userId,
        },
      ],
      currentPage: 1,
      pageSize: 1000,
    },
  );
}

/**
 * 添加项目到用户
 */
export async function addProjectToUserApi(
  enterpriseIdList: string[],
  userId: string,
  roleTypes?: string,
  expiredTime?: string,
) {
  return requestClient.post<UserApi.UserResult>(
    '/api/user/doAddUserEnterprise',
    {
      enterpriseIdList,
      userId,
      roleTypes,
      expiredTime,
    },
  );
}

/**
 * 获取可用项目
 */
export async function getAvailableProjectsApi(
  data: UserApi.GetMyApplicationListParams,
) {
  return requestClient.post<UserApi.GetMyApplicationListResult>(
    '/api/enterprise/myList',
    data,
  );
}

/**
 * 获取企业客户列表
 */
export async function getEnterpriseCustomersApi(
  data: UserApi.GetEnterpriseCustomersParams,
) {
  return requestClient.post<UserApi.GetEnterpriseCustomersResult>(
    '/api/enterclient/list',
    data,
  );
}

/**
 * 添加企业客户
 */
export async function addEnterpriseCustomerApi(
  data: UserApi.EnterpriseCustomerParams,
) {
  return requestClient.post<UserApi.EnterpriseCustomerResult>(
    '/api/enterclient/doCreate',
    data,
  );
}

/**
 * 更新企业客户
 */
export async function updateEnterpriseCustomerApi(
  data: UserApi.EnterpriseCustomerParams,
) {
  return requestClient.post<UserApi.EnterpriseCustomerResult>(
    '/api/enterclient/doUpdate',
    data,
  );
}

/**
 * 删除企业客户
 */
export async function deleteEnterpriseCustomerApi(data: { code: string }) {
  return requestClient.post<UserApi.EnterpriseCustomerResult>(
    '/api/enterclient/doDelete',
    data,
  );
}

/**
 * 获取菜单权限按钮
 */
export async function getMenuPermissionButtonsApi(
  data: UserApi.GetMenuPermissionButtonsParams,
) {
  return requestClient.post<UserApi.GetMenuPermissionButtonsResult>(
    '/api/menubutton/getMenuPermissionButtons',
    data,
  );
}
