import { apiRequest } from '../lib/api' import { getActiveBusinessId } from '../lib/businessContext' export interface RollCallTask { id: string title: string sortOrder: number } export interface RollCall { id: string businessId: string userId: string workDate: string startTime: string endTime: string workingMinutes: number taskCount: number tasks: RollCallTask[] createdAt: string updatedAt: string } export interface RollCallSummary { totalDays: number totalWorkingMinutes: number totalTasks: number } export interface RollCallsListResponse { items: RollCall[] total: number page: number pageSize: number summary: RollCallSummary } export interface ListRollCallsParams { dateFrom?: string dateTo?: string page?: number pageSize?: number } export interface RollCallInput { workDate: string startTime: string endTime: string tasks?: { title: string }[] } export type RollCallPatch = Partial function businessPath(suffix = '') { const businessId = getActiveBusinessId() if (!businessId) { throw new Error('No active business selected. Please sign in again.') } return `/businesses/${businessId}/roll-calls${suffix}` } export async function listRollCalls( params: ListRollCallsParams = {}, signal?: AbortSignal, ) { const q = new URLSearchParams() if (params.dateFrom) q.set('dateFrom', params.dateFrom) if (params.dateTo) q.set('dateTo', params.dateTo) if (params.page !== undefined) q.set('page', String(params.page)) if (params.pageSize !== undefined) q.set('pageSize', String(params.pageSize)) const query = q.toString() return apiRequest( `${businessPath()}${query ? `?${query}` : ''}`, { auth: true, signal }, ) } export async function createRollCall(input: RollCallInput) { return apiRequest<{ rollCall: RollCall; message: string }>(businessPath(), { method: 'POST', auth: true, body: input, }) } export async function updateRollCall(rollCallId: string, input: RollCallPatch) { return apiRequest<{ rollCall: RollCall; message: string }>( businessPath(`/${rollCallId}`), { method: 'PATCH', auth: true, body: input, }, ) } export async function removeRollCall(rollCallId: string) { return apiRequest<{ message: string }>(businessPath(`/${rollCallId}`), { method: 'DELETE', auth: true, }) }