import { readingApi } from './client'
import type { ResultListTheme, ResultTheme, Theme as ApiTheme } from './generated/model'

export type Theme = ApiTheme

export interface CreateThemeData {
  name: string;
  description?: string;
  sortOrder?: number;
}

export interface UpdateThemeData {
  name?: string;
  description?: string;
  sortOrder?: number;
  status?: string;
}

// 获取主题列表
export function getThemeList() {
  return readingApi.getThemes().then((res) => {
    const wrapped = res as ResultListTheme
    return wrapped.data ?? []
  })
}

// 获取主题详情
export function getThemeDetail(id: number) {
  return readingApi.getTheme(id).then((res) => {
    const wrapped = res as ResultTheme
    return wrapped.data ?? res
  })
}

// 创建主题
export function createTheme(data: CreateThemeData) {
  return readingApi.createTheme(data as any).then((res) => res)
}

// 更新主题
export function updateTheme(id: number, data: UpdateThemeData) {
  return readingApi.updateTheme(id, data as any).then((res) => res)
}

// 删除主题
export function deleteTheme(id: number) {
  return readingApi.deleteTheme(id).then((res) => res)
}

// 重新排序主题
export function reorderThemes(ids: number[]) {
  // 暂无对应 Orval 接口，这里直接调用底层 http
  return import('./index').then(({ http }) =>
    http.put('/api/v1/admin/themes/reorder', { ids })
  )
}
