import { useQuery } from '@tanstack/react-query';
import {
  getStaffAttendanceSummary,
  listStaffAttendanceRecords,
} from '@/shared/api/staffAttendance';
import { STAFF_ATTENDANCE_QUERY_KEYS } from '@/shared/constants/staffAttendance';
import type { StaffAttendanceFilter } from '@/shared/types/staffAttendance';
import {
  toStaffAttendanceRecordViewModel,
  toStaffAttendanceSummaryViewModel,
} from '@/business/staff-attendance/mappers';
import { mapApiListRows } from '@/shared/business/apiListRows';

export function useStaffAttendanceRecords(filter?: StaffAttendanceFilter) {
  return useQuery({
    queryKey: [STAFF_ATTENDANCE_QUERY_KEYS.records, filter],
    queryFn: () => mapApiListRows(
      listStaffAttendanceRecords(filter),
      toStaffAttendanceRecordViewModel,
    ),
  });
}

export function useStaffAttendanceSummary(filter?: StaffAttendanceFilter) {
  return useQuery({
    queryKey: [STAFF_ATTENDANCE_QUERY_KEYS.summary, filter],
    queryFn: async () => {
      const response = await getStaffAttendanceSummary(filter);
      return toStaffAttendanceSummaryViewModel(response);
    },
  });
}
