import client from './client';

export interface Worldbook {
  id: number;
  userId: number;
  name: string;
  description: string;
  isPublic: boolean;
  entryCount: number;
  createdAt: string;
  updatedAt: string;
}

export interface WorldbookEntry {
  id: number;
  worldbookId: number;
  comment: string;
  content: string;
  keys: string[];
  secondaryKeys: string[];
  constant: boolean;
  enabled: boolean;
  useRegex: boolean;
  caseSensitive: boolean;
  matchWholeWords: boolean;
  selective: boolean;
  selectiveLogic: number;
  position: number;
  depth: number;
  order: number;
  probability: number;
  scanDepth: number;
  groupId: string;
  extensions?: Record<string, any>;
  createdAt: string;
  updatedAt: string;
}

export interface CreateWorldbookRequest {
  name: string;
  description?: string;
  isPublic?: boolean;
}

export interface UpdateWorldbookRequest {
  name?: string;
  description?: string;
  isPublic?: boolean;
}

export interface CreateEntryRequest {
  comment?: string;
  content: string;
  keys?: string[];
  secondaryKeys?: string[];
  constant?: boolean;
  enabled?: boolean;
  useRegex?: boolean;
  caseSensitive?: boolean;
  matchWholeWords?: boolean;
  selective?: boolean;
  selectiveLogic?: number;
  position?: number;
  depth?: number;
  order?: number;
  probability?: number;
  scanDepth?: number;
  groupId?: string;
}

export interface UpdateEntryRequest {
  comment?: string;
  content?: string;
  keys?: string[];
  secondaryKeys?: string[];
  constant?: boolean;
  enabled?: boolean;
  useRegex?: boolean;
  caseSensitive?: boolean;
  matchWholeWords?: boolean;
  selective?: boolean;
  selectiveLogic?: number;
  position?: number;
  depth?: number;
  order?: number;
  probability?: number;
  scanDepth?: number;
  groupId?: string;
}

// 创建世界书
export const createWorldbook = (data: CreateWorldbookRequest) => {
  return client.post<Worldbook>('/app/worldbook', data);
};

// 获取世界书列表
export const getWorldbookList = (params: {
  page?: number;
  pageSize?: number;
  keyword?: string;
}) => {
  return client.get<{
    list: Worldbook[];
    total: number;
    page: number;
    pageSize: number;
  }>('/app/worldbook', { params });
};

// 获取世界书详情
export const getWorldbookById = (id: number) => {
  return client.get<Worldbook>(`/app/worldbook/${id}`);
};

// 更新世界书
export const updateWorldbook = (id: number, data: UpdateWorldbookRequest) => {
  return client.put(`/app/worldbook/${id}`, data);
};

// 删除世界书
export const deleteWorldbook = (id: number) => {
  return client.delete(`/app/worldbook/${id}`);
};

// 导入世界书
export const importWorldbook = (file: File) => {
  const formData = new FormData();
  formData.append('file', file);
  return client.post<Worldbook>('/app/worldbook/import', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
  });
};

// 导出世界书
export const exportWorldbook = (id: number) => {
  return client.get(`/app/worldbook/${id}/export`, {
    responseType: 'blob',
  });
};

// 创建条目
export const createEntry = (worldbookId: number, data: CreateEntryRequest) => {
  return client.post<WorldbookEntry>(`/app/worldbook/${worldbookId}/entry`, data);
};

// 获取条目列表
export const getEntryList = (worldbookId: number) => {
  return client.get<{
    list: WorldbookEntry[];
    total: number;
  }>(`/app/worldbook/${worldbookId}/entries`);
};

// 更新条目
export const updateEntry = (worldbookId: number, entryId: number, data: UpdateEntryRequest) => {
  return client.put(`/app/worldbook/${worldbookId}/entry/${entryId}`, data);
};

// 删除条目
export const deleteEntry = (worldbookId: number, entryId: number) => {
  return client.delete(`/app/worldbook/${worldbookId}/entry/${entryId}`);
};
