import { FetchVoiceTtsV2Request } from '@/services/common'
import { GetMeetListRequest } from '@/services/home'
import { PageParams } from '@/services/im'
import { AiUserGiftsInput, AiUserInput, AiUserAlbumInput } from '@/services/user/types'

type QueryKeyFactory = string | readonly unknown[]

/**
 * 创建类型安全的查询键工厂
 */
const createQueryKeys = <
  TResource extends string,
  TKeys extends Record<string, QueryKeyFactory | ((...args: any[]) => unknown[])>,
>(
  resource: TResource,
  keys: TKeys
) => {
  return Object.entries(keys).reduce(
    (acc, [key, defaultParams]) => ({
      ...acc,
      [key]: (...params: unknown[]) => {
        if (typeof defaultParams === 'function') {
          return [resource, key, ...defaultParams(...params)]
        }
        return [resource, key, ...(Array.isArray(defaultParams) ? defaultParams : []), ...params]
      },
    }),
    {} as {
      [K in keyof TKeys]: (...params: unknown[]) => readonly [TResource, K, ...unknown[]]
    }
  )
}

export const commonKeys = createQueryKeys('common', {
  fetchVoiceTtsV2: (data: FetchVoiceTtsV2Request) => [data],
})

// 用户相关的查询键
export const userKeys = createQueryKeys('user', {
  noticeStat: 'noticeStat',
  noticeList: (data: { page: PageParams }) => [data],
  albumCreateCount: [],
})

// 帖子相关的查询键
export const postKeys = createQueryKeys('post', {
  all: [],
  detail: [],
  comments: (postId: string) => [postId],
  likes: (postId: string) => [postId],
  // 带过滤的查询键
  feed: (filter: { type: 'latest' | 'trending' | 'following' }) => [filter],
})

// 认证相关的查询键
export const authKeys = createQueryKeys('auth', {
  currentUser: 'currentUser',
  session: [],
  permissions: [],
})

export const createKeys = createQueryKeys('create', {
  dict: [],
  ai: [],
  getAICharacterList: [],
  getAI: (aiId: number) => [aiId],
})

export const aiUserKeys = createQueryKeys('aiUser', {
  baseInfo: (data: AiUserInput) => [data],
  tempBaseInfo: (data: AiUserInput) => [data],
  stat: (data: AiUserInput) => [data],
  gifts: (data: AiUserGiftsInput) => [data],
  album: (data: AiUserAlbumInput) => [data],
})

// 示例用法：
// userKeys.detail() => ["user", "detail"]
// userKeys.posts("123") => ["user", "posts", "123"]
// postKeys.feed({ type: "latest" }) => ["post", "feed", { type: "latest" }]

export const imKeys = createQueryKeys('im', {
  imAccount: [],
  imUserInfo: (aiId: number) => [aiId],
  giftList: [],
  heartbeatLevel: (aiId: number) => [aiId],
  myChatSetting: (aiId: number) => [aiId],
  chatBubbleDictList: (aiId: number) => [aiId],
  rtcToken: (roomId: string) => [roomId],
  chatBackgroundList: (aiId: number) => [aiId],
  heartbeatRelationList: (nickname: string) => [nickname],
  heartbeatRank: [],
  chatModelDictList: [],
  shareUserInfo: (aiId: number) => [aiId],
})

export const homeKeys = createQueryKeys('home', {
  getMeetList: (data: Omit<GetMeetListRequest, 'pn'>) => [data],
  getChatRank: [],
  getHeartbeatRank: [],
  getGiftRank: [],
  getSevenDaysSignList: [],
  getExplore: [],
  getHomeAiCarouselList: [],
  getHomeAggregateRecommend: [],
})

export const walletKeys = createQueryKeys('wallet', {
  getWalletBalance: [],
  getChargeProductList: [],
  getMemberDetail: [],
  getSubProductList: [],
  getTransactionList: (params: { type?: string; startTime?: number; endTime?: number }) => [params],
})
