import { apiRequest } from '@/shared/api/httpClient';
import { ApiListResponse } from '@/shared/types/api';
import type {
  StaffAttendanceFilter,
  StaffAttendanceRecordDto,
  StaffAttendanceSummaryDto,
} from '@/shared/types/staffAttendance';

const STAFF_ATTENDANCE_PATH = '/staff_attendance';

function toSearchParams(filter?: StaffAttendanceFilter): string {
  if (!filter) {
    return '';
  }

  const params = new URLSearchParams();

  if (filter.startDate) {
    params.set('startDate', filter.startDate);
  }

  if (filter.endDate) {
    params.set('endDate', filter.endDate);
  }

  if (filter.limit) {
    params.set('limit', String(filter.limit));
  }

  const query = params.toString();
  return query ? `?${query}` : '';
}

export function listStaffAttendanceRecords(
  filter?: StaffAttendanceFilter,
): Promise<ApiListResponse<StaffAttendanceRecordDto>> {
  return apiRequest<ApiListResponse<StaffAttendanceRecordDto>>(
    `${STAFF_ATTENDANCE_PATH}/records${toSearchParams(filter)}`,
  );
}

export function getStaffAttendanceSummary(filter?: StaffAttendanceFilter): Promise<StaffAttendanceSummaryDto> {
  return apiRequest<StaffAttendanceSummaryDto>(`${STAFF_ATTENDANCE_PATH}/summary${toSearchParams(filter)}`);
}
