import { legacyClient } from '../clients'

export type ListBlogItemType = {
  id: number
  title: string

  /** ISO8601 */
  created_at: string

  /** ISO8601 */
  updated_at: string

  featured_image: null | {
    image_url: string
  }

  author: {
    id: number
    username: string
    nickname: string
  }
}

export const listBlogs = async ({
  page = 1,
  limit = 20,
}: {
  page?: number
  limit?: number
}) => {
  const resp = await legacyClient.post('/v1/blog/list', { page, limit })
  return resp.data.data as ListBlogItemType[]
}

export const getBlog: (
  blog_id: number,
) => Promise<null | { title: string; content: string }> = async (
  blog_id: number,
) => {
  const resp = await legacyClient.get(`/v1/blog/${blog_id}`, {
    validateStatus: (status) => status == 200 || status == 404,
  })
  if (resp.status == 404) {
    return null
  }
  return resp.data as {
    title: string
    content: string
  }
}
