import { getSession } from '@lib/auth/session';
import { queryOptions } from '@tanstack/react-query';
import { getAllAudit } from './audit.api';
import { getAllBox } from './box.api';
import {
  getAllHouse,
  getCurrentUserHouses,
  getHouseForSelect,
} from './house.api';
import { getAllNotifications, getTopFiveNotification } from './notify.api';
import {
  getAdminSettings,
  getCurrentUserLanguage,
  getUserSettings,
} from './setting.api';
import { getAllUser, getUserForSelect } from './user.api';

export const sessionQueries = {
  all: ['auth'],
  user: () =>
    queryOptions({
      queryKey: [...sessionQueries.all, 'session'],
      queryFn: () => getSession(),
      staleTime: 1000 * 60 * 20,
      retry: false,
    }),
};

export const settingQueries = {
  all: ['setting'],
  listAdmin: () =>
    queryOptions({
      queryKey: [...settingQueries.all, 'list'],
      queryFn: () => getAdminSettings(),
    }),
  listUser: () =>
    queryOptions({
      queryKey: [...settingQueries.all, 'listUser'],
      queryFn: () => getUserSettings(),
    }),
  getCurrentUserLanguageSetting: () =>
    queryOptions({
      queryKey: [...settingQueries.all, 'language'],
      queryFn: () => getCurrentUserLanguage(),
    }),
};

export const auditQueries = {
  all: ['audit'],
  list: (params: { page: number; limit: number; keyword?: string }) =>
    queryOptions({
      queryKey: [...auditQueries.all, 'list', params],
      queryFn: () => getAllAudit({ data: params }),
    }),
};

export const usersQueries = {
  all: ['users'],
  list: (params: { page: number; limit: number; keyword?: string }) =>
    queryOptions({
      queryKey: [...usersQueries.all, 'list', params],
      queryFn: () => getAllUser({ data: params }),
    }),
  select: (params: { keyword?: string }, noself: boolean = false) =>
    queryOptions({
      queryKey: [...usersQueries.all, 'select', params],
      queryFn: () => getUserForSelect({ data: { ...params, noself } }),
    }),
};

export const housesQueries = {
  all: ['houses'],
  list: (params: { page: number; limit: number; keyword?: string }) =>
    queryOptions({
      queryKey: [...housesQueries.all, 'list', params],
      queryFn: () => getAllHouse({ data: params }),
    }),
  currentUser: () =>
    queryOptions({
      queryKey: [...housesQueries.all, 'currentUser'],
      queryFn: () => getCurrentUserHouses(),
    }),
  select: (params: { keyword?: string }) =>
    queryOptions({
      queryKey: [...housesQueries.all, 'select', params],
      queryFn: () => getHouseForSelect({ data: params }),
    }),
};

export const notificationQueries = {
  all: ['notification'],
  list: (params: { page: number; limit: number }) =>
    queryOptions({
      queryKey: [...notificationQueries.all, 'list', params],
      queryFn: () => getAllNotifications({ data: params }),
    }),
  topFive: () =>
    queryOptions({
      queryKey: [...notificationQueries.all, 'topFive'],
      queryFn: () => getTopFiveNotification(),
    }),
};

export const boxQueries = {
  all: ['boxes'],
  list: (params: { page: number; limit: number; keyword?: string }) =>
    queryOptions({
      queryKey: [...boxQueries.all, 'list', params],
      queryFn: () => getAllBox({ data: params }),
    }),
};
