import { requestJson } from './apiClient'; function normalizeItems(items) { if (Array.isArray(items)) return items; if (items && typeof items === 'object') return [items]; return []; } export const ContentService = { async listContents({ page, limit, id, tenant_id, tenant_code, tenant_name, user_id, username, keyword, status, visibility, published_at_from, published_at_to, created_at_from, created_at_to, price_amount_min, price_amount_max, sortField, sortOrder } = {}) { const iso = (d) => { if (!d) return undefined; const date = d instanceof Date ? d : new Date(d); if (Number.isNaN(date.getTime())) return undefined; return date.toISOString(); }; const query = { page, limit, id, tenant_id, tenant_code, tenant_name, user_id, username, keyword, status, visibility, published_at_from: iso(published_at_from), published_at_to: iso(published_at_to), created_at_from: iso(created_at_from), created_at_to: iso(created_at_to), price_amount_min, price_amount_max }; if (sortField && sortOrder) { if (sortOrder === 1) query.asc = sortField; if (sortOrder === -1) query.desc = sortField; } const data = await requestJson('/super/v1/contents', { query }); return { page: data?.page ?? page ?? 1, limit: data?.limit ?? limit ?? 10, total: data?.total ?? 0, items: normalizeItems(data?.items) }; }, async listTenantContents(tenantID, { page, limit, keyword, status, visibility, user_id, published_at_from, published_at_to, created_at_from, created_at_to, sortField, sortOrder } = {}) { if (!tenantID) throw new Error('tenantID is required'); const iso = (d) => { if (!d) return undefined; const date = d instanceof Date ? d : new Date(d); if (Number.isNaN(date.getTime())) return undefined; return date.toISOString(); }; const query = { page, limit, keyword, status, visibility, user_id, published_at_from: iso(published_at_from), published_at_to: iso(published_at_to), created_at_from: iso(created_at_from), created_at_to: iso(created_at_to) }; if (sortField && sortOrder) { if (sortOrder === 1) query.asc = sortField; if (sortOrder === -1) query.desc = sortField; } const data = await requestJson(`/super/v1/tenants/${tenantID}/contents`, { query }); return { page: data?.page ?? page ?? 1, limit: data?.limit ?? limit ?? 10, total: data?.total ?? 0, items: normalizeItems(data?.items) }; }, async updateTenantContentStatus(tenantID, contentID, { status } = {}) { if (!tenantID) throw new Error('tenantID is required'); if (!contentID) throw new Error('contentID is required'); return requestJson(`/super/v1/tenants/${tenantID}/contents/${contentID}/status`, { method: 'PATCH', body: { status } }); }, async reviewContent(contentID, { action, reason } = {}) { if (!contentID) throw new Error('contentID is required'); return requestJson(`/super/v1/contents/${contentID}/review`, { method: 'POST', body: { action, reason } }); }, async batchReviewContents({ content_ids, action, reason } = {}) { if (!Array.isArray(content_ids) || content_ids.length === 0) throw new Error('content_ids is required'); return requestJson('/super/v1/contents/review/batch', { method: 'POST', body: { content_ids, action, reason } }); }, async batchUpdateContentStatus({ content_ids, status, reason } = {}) { if (!Array.isArray(content_ids) || content_ids.length === 0) throw new Error('content_ids is required'); return requestJson('/super/v1/contents/status/batch', { method: 'POST', body: { content_ids, status, reason } }); }, async getContentStatistics({ tenant_id, start_at, end_at, granularity } = {}) { const iso = (d) => { if (!d) return undefined; const date = d instanceof Date ? d : new Date(d); if (Number.isNaN(date.getTime())) return undefined; return date.toISOString(); }; const query = { tenant_id, start_at: iso(start_at), end_at: iso(end_at), granularity }; return requestJson('/super/v1/contents/statistics', { query }); }, async listComments({ page, limit, id, tenant_id, tenant_code, tenant_name, content_id, content_title, user_id, username, keyword, status, created_at_from, created_at_to, sortField, sortOrder } = {}) { const iso = (d) => { if (!d) return undefined; const date = d instanceof Date ? d : new Date(d); if (Number.isNaN(date.getTime())) return undefined; return date.toISOString(); }; const query = { page, limit, id, tenant_id, tenant_code, tenant_name, content_id, content_title, user_id, username, keyword, status, created_at_from: iso(created_at_from), created_at_to: iso(created_at_to) }; if (sortField && sortOrder) { if (sortOrder === 1) query.asc = sortField; if (sortOrder === -1) query.desc = sortField; } const data = await requestJson('/super/v1/comments', { query }); return { page: data?.page ?? page ?? 1, limit: data?.limit ?? limit ?? 10, total: data?.total ?? 0, items: normalizeItems(data?.items) }; }, async deleteComment(id, { reason } = {}) { if (!id) throw new Error('id is required'); return requestJson(`/super/v1/comments/${id}/delete`, { method: 'POST', body: { reason } }); }, async listContentReports({ page, limit, id, tenant_id, tenant_code, tenant_name, content_id, content_title, reporter_id, reporter_name, handled_by, handled_by_name, reason, keyword, status, created_at_from, created_at_to, handled_at_from, handled_at_to, sortField, sortOrder } = {}) { const iso = (d) => { if (!d) return undefined; const date = d instanceof Date ? d : new Date(d); if (Number.isNaN(date.getTime())) return undefined; return date.toISOString(); }; const query = { page, limit, id, tenant_id, tenant_code, tenant_name, content_id, content_title, reporter_id, reporter_name, handled_by, handled_by_name, reason, keyword, status, created_at_from: iso(created_at_from), created_at_to: iso(created_at_to), handled_at_from: iso(handled_at_from), handled_at_to: iso(handled_at_to) }; if (sortField && sortOrder) { if (sortOrder === 1) query.asc = sortField; if (sortOrder === -1) query.desc = sortField; } const data = await requestJson('/super/v1/content-reports', { query }); return { page: data?.page ?? page ?? 1, limit: data?.limit ?? limit ?? 10, total: data?.total ?? 0, items: normalizeItems(data?.items) }; }, async processContentReport(id, { action, content_action, reason } = {}) { if (!id) throw new Error('id is required'); return requestJson(`/super/v1/content-reports/${id}/process`, { method: 'POST', body: { action, content_action, reason } }); }, async batchProcessContentReports({ report_ids, action, content_action, reason } = {}) { if (!Array.isArray(report_ids) || report_ids.length === 0) { throw new Error('report_ids is required'); } return requestJson('/super/v1/content-reports/process/batch', { method: 'POST', body: { report_ids, action, content_action, reason } }); } };