import { api, API_BASE_URL } from "@/lib/api-client"; import type { CourseChapter, ChapterMaterial, ChapterProgress, ChapterCreateRequest, WorkbenchGenerateRequest, WorkbenchGeneratedOutline, WorkbenchGeneratedChapter, } from "@/types/courseware"; import type { ApiSuccessResponse } from "@/types"; const blobDownloadHeaders = (): Record => ({ Authorization: `Bearer ${localStorage.getItem("encoach_token") ?? ""}`, }); export const coursewareService = { async listChapters(courseId: number): Promise { return api.get(`/courses/${courseId}/chapters`); }, async createChapter(courseId: number, data: ChapterCreateRequest): Promise { return api.post(`/courses/${courseId}/chapters`, data); }, async getChapter(id: number): Promise { return api.get(`/chapters/${id}`); }, async updateChapter(id: number, data: Partial): Promise { return api.patch(`/chapters/${id}`, data); }, async deleteChapter(id: number): Promise { return api.delete(`/chapters/${id}`); }, async unlockChapter(id: number): Promise { return api.post(`/chapters/${id}/unlock`); }, async lockChapter(id: number): Promise { return api.post(`/chapters/${id}/lock`); }, async reorderChapters(courseId: number, body: { ids: number[] }): Promise { return api.patch(`/courses/${courseId}/chapters/reorder`, body); }, async getChapterProgress(id: number): Promise { return api.get(`/chapters/${id}/progress`); }, async markChapterComplete(id: number): Promise { return api.post(`/chapters/${id}/progress/complete`); }, async listMaterials(chapterId: number): Promise { return api.get(`/chapters/${chapterId}/materials`); }, async uploadMaterial(chapterId: number, formData: FormData): Promise { return api.upload(`/chapters/${chapterId}/materials`, formData); }, async updateMaterial(id: number, data: Partial): Promise { return api.patch(`/materials/${id}`, data); }, async deleteMaterial(id: number): Promise { return api.delete(`/materials/${id}`); }, async downloadMaterial(id: number): Promise { const res = await fetch(`${API_BASE_URL}/materials/${id}/download`, { headers: blobDownloadHeaders(), }); if (!res.ok) throw new Error(`Download failed: ${res.status} ${res.statusText}`); return res.blob(); }, async markMaterialViewed(id: number): Promise { return api.post(`/materials/${id}/viewed`); }, async reorderMaterials(chapterId: number, body: { ids: number[] }): Promise { return api.patch(`/chapters/${chapterId}/materials/reorder`, body); }, async generateOutline(data: WorkbenchGenerateRequest): Promise { return api.post("/workbench/generate-outline", data); }, async generateChapterContent(data: WorkbenchGenerateRequest): Promise { return api.post("/workbench/generate-chapter", data); }, async generateRubric(data: WorkbenchGenerateRequest): Promise<{ rubric: string }> { return api.post("/workbench/generate-rubric", data); }, async regenerateSection(data: Record): Promise { return api.post("/workbench/regenerate", data); }, async suggestMaterials(data: Record): Promise { return api.post("/workbench/suggest-materials", data); }, async publishWorkbench(data: Record): Promise { return api.post("/workbench/publish", data); }, };