import { get, post, put, del } from './http'; export type AdminLoginResponse = { data: { key: string; }; }; export type CommentItem = { id: number; created: number; name: string; email: string; avatar: string; postSlug: string; url: string | null; ipAddress: string | null; contentText: string; contentHtml: string; status: string; ua?: string | null; }; export type CommentListResponse = { data: CommentItem[]; pagination: { page: number; limit: number; total: number; }; }; export type AdminEmailResponse = { email: string | null; }; export type CommentSettingsResponse = { adminEmail: string | null; adminBadge: string | null; avatarPrefix: string | null; adminEnabled: boolean; allowedDomains?: string[]; adminKey?: string | null; adminKeySet?: boolean; requireReview?: boolean; blockedIps?: string[]; blockedEmails?: string[]; }; export type EmailNotifySettingsResponse = { globalEnabled: boolean; smtp?: { host: string; port: number; user: string; pass: string; secure: boolean; }; templates?: { reply?: string; admin?: string; }; }; export async function loginAdmin(name: string, password: string): Promise { const res = await post('/admin/login', { name, password }); const key = res.data.key; localStorage.setItem('cwd_admin_token', key); return key; } export function logoutAdmin(): void { localStorage.removeItem('cwd_admin_token'); } export function fetchComments(page: number): Promise { return get(`/admin/comments/list?page=${page}`); } export function deleteComment(id: number): Promise<{ message: string }> { return del<{ message: string }>(`/admin/comments/delete?id=${id}`); } export function updateCommentStatus(id: number, status: string): Promise<{ message: string }> { return put<{ message: string }>(`/admin/comments/status?id=${id}&status=${encodeURIComponent(status)}`); } export function fetchAdminEmail(): Promise { return get('/admin/settings/email'); } export function saveAdminEmail(email: string): Promise<{ message: string }> { return put<{ message: string }>('/admin/settings/email', { email }); } export function fetchEmailNotifySettings(): Promise { return get('/admin/settings/email-notify'); } export function saveEmailNotifySettings(data: { globalEnabled?: boolean; smtp?: { host?: string; port?: number; user?: string; pass?: string; secure?: boolean; }; templates?: { reply?: string; admin?: string; }; }): Promise<{ message: string }> { return put<{ message: string }>('/admin/settings/email-notify', data); } export function sendTestEmail(data: { toEmail: string; smtp?: { host?: string; port?: number; user?: string; pass?: string; secure?: boolean; }; }): Promise<{ message: string }> { return post<{ message: string }>('/admin/settings/email-test', data); } export function fetchCommentSettings(): Promise { return get('/admin/settings/comments'); } export function saveCommentSettings(data: { adminEmail?: string; adminBadge?: string; avatarPrefix?: string; adminEnabled?: boolean; allowedDomains?: string[]; adminKey?: string; requireReview?: boolean; blockedIps?: string[]; blockedEmails?: string[]; }): Promise<{ message: string }> { return put<{ message: string }>('/admin/settings/comments', data); } export function blockIp(ip: string): Promise<{ message: string }> { return post<{ message: string }>('/admin/comments/block-ip', { ip }); } export function blockEmail(email: string): Promise<{ message: string }> { return post<{ message: string }>('/admin/comments/block-email', { email }); } export function exportComments(): Promise { return get('/admin/comments/export'); } export function importComments(data: any[]): Promise<{ message: string }> { return post<{ message: string }>('/admin/comments/import', data); }