import { http } from './client'
import type { Bookmark, CreateBookmarkRequest, UpdateBookmarkRequest, VisibilityType } from '@/types'

export const bookmarkApi = {
  /**
   * 获取书签列表
   */
  async getBookmarks(tag?: string) {
    const params = tag ? { tag } : undefined
    return http.get<Bookmark[]>('/bookmarks', { params })
  },

  /**
   * 搜索书签
   */
  async searchBookmarks(keyword: string) {
    return http.get<Bookmark[]>('/bookmarks/search', { params: { keyword } })
  },

  /**
   * 获取书签详情
   */
  async getBookmark(id: string) {
    return http.get<Bookmark>(`/bookmarks/${id}`)
  },

  /**
   * 创建书签
   */
  async createBookmark(data: CreateBookmarkRequest) {
    return http.post<Bookmark>('/bookmarks', data)
  },

  /**
   * 更新书签
   */
  async updateBookmark(id: string, data: UpdateBookmarkRequest) {
    return http.put<Bookmark>(`/bookmarks/${id}`, data)
  },

  /**
   * 删除书签
   */
  async deleteBookmark(id: string) {
    return http.delete(`/bookmarks/${id}`)
  },

  /**
   * 批量删除书签
   */
  async batchDeleteBookmarks(ids: string[]) {
    return http.post<number>('/bookmarks/batch/delete', ids)
  },

  /**
   * 设置可见性
   */
  async setVisibility(id: string, visibility: VisibilityType, allowedDevices?: string[]) {
    return http.put(`/bookmarks/${id}/visibility`, { visibility, allowedDevices })
  },

  /**
   * 记录访问
   */
  async recordVisit(id: string) {
    return http.post(`/bookmarks/${id}/visit`)
  },

  /**
   * 更新排序
   */
  async updateOrder(id: string, order: number) {
    return http.put(`/bookmarks/${id}/order`, { order })
  },

  /**
   * 检查 URL 是否存在
   */
  async checkUrl(url: string) {
    return http.get<boolean>('/bookmarks/check-url', { params: { url } })
  },

  /**
   * 导入书签
   */
  async importBookmarks(bookmarks: CreateBookmarkRequest[]) {
    return http.post<number>('/bookmarks/import', bookmarks)
  },

  /**
   * 导出书签
   */
  async exportBookmarks() {
    return http.get<Bookmark[]>('/bookmarks/export')
  }
}

