import { QueryClient } from '@tanstack/vue-query'

export const queryKeys = {
  taskBoard: (doneLimit = 50) => ['tasks', 'board', { doneLimit }] as const,
  task: (id: string) => ['tasks', 'detail', id] as const,
  taskChildren: (id: string) => ['tasks', 'detail', id, 'children'] as const,
  taskActivity: (id: string) => ['tasks', 'detail', id, 'activity'] as const,
  taskRuns: (id: string) => ['openclaw', 'runs', { taskId: id, limit: 20 }] as const,
  projects: () => ['projects'] as const,
  project: (id: string) => ['projects', id] as const,
  projectTasks: (id: string) => ['projects', id, 'tasks'] as const,
  agentProposals: (filters?: { limit?: number; cursor?: string; status?: string }) => filters
    ? ['openclaw', 'agent-proposals', filters] as const
    : ['openclaw', 'agent-proposals'] as const,
  agentProposal: (id: string) => ['openclaw', 'agent-proposals', id] as const,
  agentCreateOptions: () => ['openclaw', 'agents', 'create-options'] as const,
  agentDetail: (id: string) => ['agents', 'detail', id] as const,
  agentActivity: (id: string) => ['agents', 'detail', id, 'activity'] as const,
  agentSummary: (id: string) => ['agents', 'detail', id, 'summary'] as const,
  agentFiles: (id: string) => ['openclaw', 'agents', id, 'files'] as const,
  agentFile: (id: string, fileName: string) =>
    ['openclaw', 'agents', id, 'files', fileName] as const,
  openClawOverview: () => ['openclaw', 'overview'] as const,
  openClawAgents: () => ['openclaw', 'agents'] as const,
  openClawModelAuth: () => ['openclaw', 'models', 'auth-status'] as const,
  openClawRuns: (filters?: {
    limit?: number
    cursor?: string | null
    status?: string | null
    taskId?: string | null
    projectId?: string | null
    sessionKey?: string | null
  }) => filters
    ? ['openclaw', 'runs', filters] as const
    : ['openclaw', 'runs'] as const,
  openClawRun: (id: string) => ['openclaw', 'runs', 'detail', id] as const,
  openClawRunHistory: (id: string) => ['openclaw', 'runs', 'history', id] as const,
  openClawCron: (filters?: { includeDisabled?: boolean; limit?: number }) => filters
    ? ['openclaw', 'cron', filters] as const
    : ['openclaw', 'cron'] as const,
  openClawCronDetail: (id: string) => ['openclaw', 'cron', 'detail', id] as const,
  openClawCronRuns: (id: string, runId?: string | null) =>
    ['openclaw', 'cron', 'runs', id, { runId: runId ?? null }] as const,
  openClawCapabilities: () => ['openclaw', 'capabilities'] as const,
  notifications: (forUser = 'bao', limit = 50, unreadOnly = false) =>
    ['notifications', { forUser, limit, unreadOnly }] as const,
  activity: (pageSize = 200) => ['activity', { pageSize }] as const,
  docs: (agentId = 'iris') => ['openclaw', 'agents', agentId, 'docs'] as const,
  doc: (agentId: string, path: string) =>
    ['openclaw', 'agents', agentId, 'docs', path] as const,
  memory: (agentId = 'iris') =>
    ['openclaw', 'agents', agentId, 'memory'] as const,
  memoryFile: (agentId: string, name: string) =>
    ['openclaw', 'agents', agentId, 'memory', name] as const,
  memorySearch: (agentId: string, query: string) =>
    ['openclaw', 'agents', agentId, 'memory', 'search', query] as const,
  incidents: (agentId = 'iris') =>
    ['openclaw', 'agents', agentId, 'incidents'] as const,
  incident: (agentId: string, name: string) =>
    ['openclaw', 'agents', agentId, 'incidents', name] as const,
  securityStatus: () => ['security', 'status'] as const,
}

export const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      staleTime: 15_000,
      gcTime: 5 * 60_000,
      refetchOnWindowFocus: true,
      retry(failureCount, error) {
        const status = typeof error === 'object' && error && 'status' in error
          ? Number((error as { status?: unknown }).status)
          : 0
        if (status >= 400 && status < 500) return false
        return failureCount < 2
      },
    },
    mutations: {
      retry: false,
    },
  },
})
