import { apiRequest, type ApiSession } from './client'

export type DatasetSourceKind = 'manual' | 'link' | 'rss'

export type DatasetSourceDTO = {
  id: string
  name: string
  kind: DatasetSourceKind
  url: string
  iconUrl: string
  description: string
  enabled: boolean
  builtIn: boolean
  lastSyncedAt: string | null
  itemCount: number
  createdAt: string
  updatedAt: string
}

export type DatasetItemDTO = {
  id: string
  sourceId: string
  title: string
  summary: string
  content: string
  url: string
  status: 'unread' | 'read' | 'archived'
  starred: boolean
  publishedAt: string | null
  createdAt: string
  updatedAt: string
}

export type DatasetCronDTO = {
  id: string
  sourceId: string
  status: string
  lastRunAt: string | null
  lastResult: string
  createdAt: string
  updatedAt: string
}

export type DatasetItemPageDTO = {
  items: DatasetItemDTO[]
  total: number
  limit: number
  offset: number
}

export type DatasetDepositDTO = {
  noteId: string
  projectId: string
}

export type DatasetSourceInput = {
  name: string
  kind: DatasetSourceKind
  url: string
  iconUrl: string
  description: string
  enabled: boolean
}

export type DatasetItemUpdate = {
  status?: DatasetItemDTO['status']
  starred?: boolean
}

export function listDatasetSources(session: ApiSession, signal?: AbortSignal) {
  return apiRequest<DatasetSourceDTO[]>('/api/v1/dataset-sources', { token: session.token, signal })
}

export function createDatasetSource(session: ApiSession, input: DatasetSourceInput) {
  return apiRequest<DatasetSourceDTO>('/api/v1/dataset-sources', {
    method: 'POST',
    token: session.token,
    body: input,
  })
}

export function updateDatasetSource(session: ApiSession, sourceId: string, input: DatasetSourceInput) {
  return apiRequest<DatasetSourceDTO>(`/api/v1/dataset-sources/${sourceId}`, {
    method: 'PATCH',
    token: session.token,
    body: input,
  })
}

export function deleteDatasetSource(session: ApiSession, sourceId: string) {
  return apiRequest<void>(`/api/v1/dataset-sources/${sourceId}`, {
    method: 'DELETE',
    token: session.token,
    responseType: 'void',
  })
}

export function listDatasetItems(
  session: ApiSession,
  sourceId?: string,
  offset = 0,
  limit = 50,
  signal?: AbortSignal,
) {
  const params = new URLSearchParams({ offset: String(offset), limit: String(limit) })
  if (sourceId) params.set('sourceId', sourceId)
  return apiRequest<DatasetItemPageDTO>(`/api/v1/dataset-items?${params}`, { token: session.token, signal })
}

export function updateDatasetItem(session: ApiSession, itemId: string, input: DatasetItemUpdate) {
  return apiRequest<DatasetItemDTO>(`/api/v1/dataset-items/${itemId}`, {
    method: 'PATCH',
    token: session.token,
    body: input,
  })
}

export function queueDatasetSync(session: ApiSession) {
  return apiRequest<DatasetCronDTO[]>('/api/v1/dataset-crons/sync', {
    method: 'POST',
    token: session.token,
    body: {},
  })
}

export function listDatasetCrons(session: ApiSession, signal?: AbortSignal) {
  return apiRequest<DatasetCronDTO[]>('/api/v1/dataset-crons', { token: session.token, signal })
}

export function depositDatasetItem(session: ApiSession, itemId: string, projectId: string) {
  return apiRequest<DatasetDepositDTO>(`/api/v1/dataset-items/${itemId}/deposit`, {
    method: 'POST',
    token: session.token,
    body: { projectId },
  })
}
