import { queryOptions } from '@tanstack/react-query';
import {
  getCertificates,
  getTrainingRecordCourseOptions,
  getUserOptions,
  getTrainingRecordById,
  getTrainingRecords
} from './service';
import type {
  Certificate,
  TrainingRecord,
  TrainingRecordCourseOption,
  TrainingRecordFilters
} from './types';

export type { Certificate, TrainingRecord, TrainingRecordCourseOption };

const trainingRecordAllKey = ['training-records'] as const;

export const trainingRecordKeys = {
  all: trainingRecordAllKey,
  list: (filters: TrainingRecordFilters) =>
    [...trainingRecordAllKey, 'list', filters] as const,
  detail: (id: number) => [...trainingRecordAllKey, 'detail', id] as const,
  certificates: (trainingRecordId: number) =>
    [...trainingRecordAllKey, 'certificates', trainingRecordId] as const,
  users: [...trainingRecordAllKey, 'user-options'] as const,
  courses: (search: string) => [...trainingRecordAllKey, 'course-options', search] as const
};

export const trainingRecordsQueryOptions = (filters: TrainingRecordFilters) =>
  queryOptions({
    queryKey: trainingRecordKeys.list(filters),
    queryFn: () => getTrainingRecords(filters)
  });

export const trainingRecordByIdOptions = (id: number) =>
  queryOptions({
    queryKey: trainingRecordKeys.detail(id),
    queryFn: () => getTrainingRecordById(id)
  });

export const certificatesQueryOptions = (trainingRecordId: number) =>
  queryOptions({
    queryKey: trainingRecordKeys.certificates(trainingRecordId),
    queryFn: () => getCertificates(trainingRecordId)
  });

export const userOptionsQueryOptions = () =>
  queryOptions({
    queryKey: trainingRecordKeys.users,
    queryFn: getUserOptions
  });

export const trainingRecordCourseOptionsQueryOptions = (search: string) =>
  queryOptions({
    queryKey: trainingRecordKeys.courses(search),
    queryFn: () => getTrainingRecordCourseOptions(search)
  });
