import { request } from '@umijs/max';
import type { ArticleItem, ArticleParams } from '@/pages/Article/data';

/** 获取文章列表 GET /api/articles */
export async function queryArticleList(params: ArticleParams) {
    return request<{
        data: ArticleItem[];
        total: number;
        success: boolean;
    }>('/api/articles', {
        method: 'GET',
        params,
    });
}

/** 获取单篇文章 GET /api/articles/:id */
export async function getArticle(id: string) {
    return request<{
        data: ArticleItem;
        success: boolean;
    }>(`/api/articles/${id}`, {
        method: 'GET',
    });
}

/** 新建文章 POST /api/articles */
export async function addArticle(data: Partial<ArticleItem>) {
    return request<{
        data: ArticleItem;
        success: boolean;
    }>('/api/articles', {
        method: 'POST',
        data,
    });
}

/** 更新文章 PUT /api/articles/:id */
export async function updateArticle(id: string, data: Partial<ArticleItem>) {
    return request<{
        data: ArticleItem;
        success: boolean;
    }>(`/api/articles/${id}`, {
        method: 'PUT',
        data,
    });
}

/** 删除文章 DELETE /api/articles/:id */
export async function removeArticle(id: string) {
    return request<{
        success: boolean;
    }>(`/api/articles/${id}`, {
        method: 'DELETE',
    });
}
