import api from './api';
import request, { post } from '@/utils/request';
import type { ITenantInfo } from '@/interfaces/database/knowledge';
import type { IUserInfo, ITenant } from '@/interfaces/database/user-setting';
import type { LlmModelType } from '@/constants/knowledge';
import type { IAddLlmRequestBody, IDeleteLlmRequestBody, ISetApiKeyRequestBody } from '@/interfaces/request/llm';
import type { IMcpServer } from '@/interfaces/database/mcp';
import type { ICreateMcpServerRequestBody, IImportMcpServersRequestBody, ITestMcpRequestBody } from '@/interfaces/request/mcp';
import type { IPaginationBody, IPaginationRequestBody } from '@/interfaces/request/base';

// 用户相关API服务
const userService = {
  // 用户登录
  login: (data: { email: string; password: string }) => {
    return post(api.login, data);
  },

  // 用户登出
  logout: () => {
    return request.get(api.logout);
  },

  // 用户注册
  register: (data: { email: string; password: string; nickname?: string }) => {
    return post(api.register, data);
  },

  // 获取用户信息
  getUserInfo: () => {
    return request.get(api.user_info);
  },

  // 更新用户密码
  updatePassword: (data: { password: string; new_password: string }) => {
    return post(api.setting, data);
  },

  // 更新用户设置
  updateSetting: (data: Partial<IUserInfo>) => {
    return post(api.setting, data);
  },

  // 获取登录渠道
  getLoginChannels: () => {
    return request.get(api.login_channels);
  },

  // 通过渠道登录
  loginWithChannel: (channel: string) => {
    window.location.href = api.login_channel(channel);
  },

  /** 租户用户管理接口 */
  // 获取租户信息
  getTenantInfo: () => {
    return request.get(api.tenant_info);
  },

  // 设置租户信息
  setTenantInfo: (data: Partial<Omit<ITenantInfo, 'role'>>) => {
    return post(api.set_tenant_info, data);
  },
  // 租户用户管理
  listTenantUser: (tenantId: string) => {
    return request.get(api.listTenantUser(tenantId));
  },

  addTenantUser: (tenantId: string, email: string) => {
    return post(api.addTenantUser(tenantId), { email });
  },

  deleteTenantUser: ({ tenantId, userId }: { tenantId: string; userId: string }) => {
    return request.delete(api.deleteTenantUser(tenantId, userId));
  },

  // 租户管理
  listTenant: () => {
    return request.get(api.listTenant);
  },

  agreeTenant: (tenantId: string) => {
    return request.put(api.agreeTenant(tenantId));
  },

  /** LLM 模型相关接口 */
  // llm list
  llm_list: (data: { model_type?: LlmModelType }) => {
    return request.get(api.llm_list, { params: data });
  },

  // my llm list
  my_llm: (params: { include_details?: boolean } = {}) => {
    return request.get(api.my_llm, { params: { include_details: params.include_details } });
  },

  // llm factory list
  llm_factories_list: () => {
    return request.get(api.factories_list);
  },

  // add llm
  add_llm: (data: Partial<IAddLlmRequestBody>) => {
    return request.post(api.add_llm, data);
  },

  // delete llm
  delete_llm: (data: IDeleteLlmRequestBody) => {
    return request.post(api.delete_llm, data);
  },

  // delete factory
  deleteFactory: (data: IDeleteLlmRequestBody) => {
    return request.post(api.deleteFactory, data);
  },

  // set api key
  set_api_key: (data: ISetApiKeyRequestBody) => {
    return request.post(api.set_api_key, data);
  },

  /* system status */

  // 获取系统状态
  system_status: () => {
    return request.get(api.getSystemStatus);
  },

  /** mcp server 相关接口 */

  // list mcp server
  listMcpServer: (params: IPaginationBody & { keyword?: string }) => {
    return request.post(api.listMcpServer, {}, { params });
  },

  // create mcp server
  createMcpServer: (data: ICreateMcpServerRequestBody) => {
    return request.post(api.createMcpServer, data);
  },

  // update mcp server
  updateMcpServer: (data: ICreateMcpServerRequestBody & { mcp_id: string }) => {
    return request.post(api.updateMcpServer, data);
  },

  // remove mcp server
  removeMcpServer: (mcp_ids: string[]) => {
    return request.post(api.deleteMcpServer, { mcp_ids });
  },

  // import mcp server
  importMcpServer: (data: IImportMcpServersRequestBody) => {
    return request.post(api.importMcpServer, data);
  },

  // export mcp server
  exportMcpServer: (mcp_ids: string[]) => {
    return request.post(api.exportMcpServer, { mcp_ids });
  },

  // mcp detail 
  mcpDetail: (mcpId: string) => {
    return request.get(api.getMcpServer, { params: { mcp_id: mcpId } });
  },

  // test mcp server
  testMcpServer: (data: ITestMcpRequestBody) => {
    return request.post(api.testMcpServer, data);
  },
};

export default userService;