import { apiRequest } from '@/shared/api/httpClient';

export interface ClassAttendanceSummaryRow {
  readonly date: string;
  readonly total_enrolled: number;
  readonly total_present: number;
  readonly total_absent: number;
  readonly total_tardy: number;
  readonly attendance_percentage: number;
}

export interface ClassAttendanceTotals {
  readonly total_enrolled: number;
  readonly total_present: number;
  readonly total_absent: number;
  readonly total_tardy: number;
}

/** Rollup totals per date for the caller's active tier (a class for a teacher). */
export function getClassAttendanceSummary(
  startDate?: string,
  endDate?: string,
): Promise<{ rows: ClassAttendanceSummaryRow[] }> {
  const search = new URLSearchParams();
  if (startDate) search.set('startDate', startDate);
  if (endDate) search.set('endDate', endDate);
  const qs = search.toString();
  return apiRequest<{ rows: ClassAttendanceSummaryRow[] }>(
    `/class_attendance/summary${qs ? `?${qs}` : ''}`,
  );
}

export function upsertClassAttendance(
  classId: string,
  date: string,
  data: ClassAttendanceTotals,
): Promise<unknown> {
  return apiRequest(`/class_attendance/${classId}/${date}`, {
    method: 'PUT',
    body: { data },
  });
}
