import type { QueryClient } from '@tanstack/react-query';
import { announcementKeys } from '@/features/announcements/api/queries';
import { courseKeys } from '@/features/courses/api/queries';
import { employeeDirectoryKeys } from '@/features/employee-directory/api/queries';
import { employeeKeys } from '@/features/employees/api/queries';
import { masterReviewKeys } from '@/features/import-employees/api/queries';
import { notificationKeys } from '@/features/notifications/api/queries';
import { onlineLessonKeys } from '@/features/online-lessons/api/queries';
import { organizerKeys } from '@/features/organizers/api/queries';
import { permissionManagementKeys } from '@/features/permission-management/api/queries';
import { trainingMatrixKeys } from '@/features/training-matrix/api/queries';
import { trainingPolicyKeys } from '@/features/training-policy/api/queries';
import { trainingRecordKeys } from '@/features/training-records/api/queries';
import { userKeys } from '@/features/users/api/queries';
import { getQueryClient } from './query-client';

const trainingRecordCourseOptionKeys = [...trainingRecordKeys.all, 'course-options'] as const;

function client(queryClient?: QueryClient) {
  return queryClient ?? getQueryClient();
}

function invalidateMany(queryClient: QueryClient, queryKeys: readonly (readonly unknown[])[]) {
  return Promise.all(
    queryKeys.map((queryKey) =>
      queryClient.invalidateQueries({
        queryKey
      })
    )
  );
}

export function invalidatePeopleData(queryClient = getQueryClient()) {
  return invalidateMany(client(queryClient), [
    userKeys.all,
    employeeKeys.all,
    employeeDirectoryKeys.all,
    trainingRecordKeys.users,
    permissionManagementKeys.users
  ]);
}

export function invalidateCourseData(queryClient = getQueryClient()) {
  return invalidateMany(client(queryClient), [
    courseKeys.all,
    trainingRecordCourseOptionKeys,
    trainingMatrixKeys.options,
    trainingMatrixKeys.all
  ]);
}

export function invalidateTrainingRecordData(queryClient = getQueryClient()) {
  return invalidateMany(client(queryClient), [
    trainingRecordKeys.all,
    employeeDirectoryKeys.all,
    trainingMatrixKeys.compliance
  ]);
}

export function invalidateTrainingMatrixData(queryClient = getQueryClient()) {
  return invalidateMany(client(queryClient), [
    trainingMatrixKeys.all,
    employeeDirectoryKeys.all,
    trainingRecordKeys.all
  ]);
}

export function invalidateContentWorkflowData(queryClient = getQueryClient()) {
  return invalidateMany(client(queryClient), [
    announcementKeys.all,
    onlineLessonKeys.all,
    notificationKeys.all
  ]);
}

export function invalidatePermissionData(queryClient = getQueryClient()) {
  return invalidateMany(client(queryClient), [
    permissionManagementKeys.all,
    userKeys.all
  ]);
}

export function invalidateOrganizerData(queryClient = getQueryClient()) {
  return invalidateMany(client(queryClient), [
    organizerKeys.all,
    userKeys.all,
    employeeKeys.all,
    employeeDirectoryKeys.all
  ]);
}

export function invalidateEmployeeImportData(queryClient = getQueryClient()) {
  return invalidateMany(client(queryClient), [
    masterReviewKeys.all,
    employeeKeys.all,
    employeeDirectoryKeys.all,
    trainingRecordKeys.users
  ]);
}

export function invalidateTrainingPolicyData(queryClient = getQueryClient()) {
  return invalidateMany(client(queryClient), [
    trainingPolicyKeys.all,
    trainingMatrixKeys.all
  ]);
}
