/**
 * 后台管理端 API（带 JWT）
 */
import { http, type Paginated } from './api';
import type {
  AdminInfo,
  AdminUser,
  LoginResult,
  Manual,
  ManualTreeNode,
  Message,
  News,
  NewsCategory,
  Product,
  ProductCategory,
  SiteConfig,
  Team,
} from './types';

export const adminApi = {
  // ---------- 鉴权 ----------
  login: (payload: { username: string; password: string }) =>
    http.post<unknown, LoginResult>('/admin/auth/login', payload),
  getProfile: () =>
    http.get<unknown, AdminInfo & { id: number }>('/admin/auth/profile'),
  changePassword: (payload: { oldPassword: string; newPassword: string }) =>
    http.post<unknown, unknown>('/admin/auth/change-password', payload),

  // ---------- 产品分类 ----------
  productCategoryAll: (params?: Record<string, unknown>) =>
    http.get<unknown, ProductCategory[]>('/admin/product-category', { params }),
  productCategoryDetail: (id: number) =>
    http.get<unknown, ProductCategory>(`/admin/product-category/${id}`),
  productCategoryCreate: (payload: Partial<ProductCategory>) =>
    http.post<unknown, ProductCategory>('/admin/product-category', payload),
  productCategoryUpdate: (id: number, payload: Partial<ProductCategory>) =>
    http.put<unknown, unknown>(`/admin/product-category/${id}`, payload),
  productCategoryDelete: (id: number) =>
    http.delete<unknown, unknown>(`/admin/product-category/${id}`),

  // ---------- 产品 ----------
  productPaginate: (params: Record<string, unknown>) =>
    http.get<unknown, Paginated<Product>>('/admin/product', { params }),
  productDetail: (id: number) =>
    http.get<unknown, Product>(`/admin/product/${id}`),
  productCreate: (payload: Partial<Product>) =>
    http.post<unknown, Product>('/admin/product', payload),
  productUpdate: (id: number, payload: Partial<Product>) =>
    http.put<unknown, unknown>(`/admin/product/${id}`, payload),
  productDelete: (id: number) =>
    http.delete<unknown, unknown>(`/admin/product/${id}`),

  // ---------- 新闻分类 ----------
  newsCategoryAll: (params?: Record<string, unknown>) =>
    http.get<unknown, NewsCategory[]>('/admin/news-category', { params }),
  newsCategoryDetail: (id: number) =>
    http.get<unknown, NewsCategory>(`/admin/news-category/${id}`),
  newsCategoryCreate: (payload: Partial<NewsCategory>) =>
    http.post<unknown, NewsCategory>('/admin/news-category', payload),
  newsCategoryUpdate: (id: number, payload: Partial<NewsCategory>) =>
    http.put<unknown, unknown>(`/admin/news-category/${id}`, payload),
  newsCategoryDelete: (id: number) =>
    http.delete<unknown, unknown>(`/admin/news-category/${id}`),

  // ---------- 新闻 ----------
  newsPaginate: (params: Record<string, unknown>) =>
    http.get<unknown, Paginated<News>>('/admin/news', { params }),
  newsDetail: (id: number) =>
    http.get<unknown, News>(`/admin/news/${id}`),
  newsCreate: (payload: Partial<News>) =>
    http.post<unknown, News>('/admin/news', payload),
  newsUpdate: (id: number, payload: Partial<News>) =>
    http.put<unknown, unknown>(`/admin/news/${id}`, payload),
  newsDelete: (id: number) =>
    http.delete<unknown, unknown>(`/admin/news/${id}`),

  // ---------- 团队 ----------
  teamAll: (params?: Record<string, unknown>) =>
    http.get<unknown, Team[]>('/admin/team', { params }),
  teamDetail: (id: number) =>
    http.get<unknown, Team>(`/admin/team/${id}`),
  teamCreate: (payload: Partial<Team>) =>
    http.post<unknown, Team>('/admin/team', payload),
  teamUpdate: (id: number, payload: Partial<Team>) =>
    http.put<unknown, unknown>(`/admin/team/${id}`, payload),
  teamDelete: (id: number) =>
    http.delete<unknown, unknown>(`/admin/team/${id}`),

  // ---------- 留言 ----------
  messagePaginate: (params: Record<string, unknown>) =>
    http.get<unknown, Paginated<Message>>('/admin/message', { params }),
  messageMarkRead: (id: number) =>
    http.put<unknown, unknown>(`/admin/message/${id}/read`),
  messageDelete: (id: number) =>
    http.delete<unknown, unknown>(`/admin/message/${id}`),

  // ---------- 网站配置 ----------
  siteConfigGet: () =>
    http.get<unknown, SiteConfig>('/admin/site-config'),
  siteConfigUpdate: (payload: Partial<SiteConfig>) =>
    http.put<unknown, SiteConfig>('/admin/site-config', payload),

  // ---------- 管理员账号 ----------
  adminUserPaginate: (params: Record<string, unknown>) =>
    http.get<unknown, Paginated<AdminUser>>('/admin/admin-user', { params }),
  adminUserDetail: (id: number) =>
    http.get<unknown, AdminUser>(`/admin/admin-user/${id}`),
  adminUserCreate: (
    payload: Partial<AdminUser> & { password: string; role?: AdminUser['role'] },
  ) => http.post<unknown, AdminUser>('/admin/admin-user', payload),
  adminUserUpdate: (
    id: number,
    payload: Pick<AdminUser, 'nickname' | 'avatar' | 'role'>,
  ) => http.put<unknown, unknown>(`/admin/admin-user/${id}`, payload),
  adminUserResetPassword: (id: number, newPassword: string) =>
    http.put<unknown, unknown>(`/admin/admin-user/${id}/password`, {
      newPassword,
    }),
  adminUserDelete: (id: number) =>
    http.delete<unknown, unknown>(`/admin/admin-user/${id}`),

  // ---------- 使用手册 ----------
  manualList: (params?: Record<string, unknown>) =>
    http.get<unknown, Manual[]>('/admin/manual', { params }),
  manualTree: () =>
    http.get<unknown, ManualTreeNode[]>('/admin/manual/tree'),
  manualDetail: (id: number) =>
    http.get<unknown, Manual>(`/admin/manual/${id}`),
  manualCreate: (payload: Partial<Manual>) =>
    http.post<unknown, Manual>('/admin/manual', payload),
  manualUpdate: (id: number, payload: Partial<Manual>) =>
    http.put<unknown, unknown>(`/admin/manual/${id}`, payload),
  manualDelete: (id: number) =>
    http.delete<unknown, unknown>(`/admin/manual/${id}`),
};
