import { apiRequestData } from "./http"; export type DepartmentItem = { id: string; name: string; parentId: string | null; archivedAt: string | null; createdAt: string | null; updatedAt: string | null; }; export type DepartmentTreeNode = DepartmentItem & { children: DepartmentTreeNode[]; }; export type DepartmentListResponse = { includeArchived: boolean; items: DepartmentItem[]; tree: DepartmentTreeNode[]; }; type DepartmentMutationResponse = { department: DepartmentItem; outcome?: "already_archived" | "archived"; }; export async function fetchDepartments(params: { includeArchived?: boolean } = {}) { const search = new URLSearchParams(); if (params.includeArchived != null) { search.set("includeArchived", params.includeArchived ? "1" : "0"); } const qs = search.toString(); return apiRequestData(`/api/departments${qs ? `?${qs}` : ""}`); } export async function createDepartment(payload: { name: string; parentId?: string | null }) { const response = await apiRequestData("/api/departments", { method: "POST", body: JSON.stringify(payload), }); return response.department; } export async function updateDepartment(departmentId: string, payload: { name?: string; parentId?: string | null }) { const response = await apiRequestData(`/api/departments/${departmentId}`, { method: "PATCH", body: JSON.stringify(payload), }); return response.department; } export async function archiveDepartment(departmentId: string) { return apiRequestData(`/api/departments/${departmentId}/archive`, { method: "POST", body: JSON.stringify({}), }); }