import { DataSource } from 'typeorm';
import {
  QueryType,
  QueryTypeKey,
  Query,
  GetMessagesQuery,
  GetMessagesSinceQuery,
  GetMessageByIdQuery,
  GetReactionsForMessageQuery,
  GetUserQuery,
  GetRoomQuery,
  GetBansForRoomQuery,
  IsUserBannedQuery,
  GetAttachmentsForMessageQuery,
  GetPluginDataQuery,
  GetMetaQuery
} from '../types';
import { handleGetMessages } from './handlers/getMessages';
import { handleGetMessagesSince } from './handlers/getMessagesSince';
import { handleGetMessageById } from './handlers/getMessageById';
import { handleGetReactionsForMessage } from './handlers/getReactionsForMessage';
import { handleGetUser } from './handlers/getUser';
import { handleGetCurrentUser } from './handlers/getCurrentUser';
import { handleGetCurrentUserId } from './handlers/getCurrentUserId';
import { handleGetUsersByRoom } from './handlers/getUsersByRoom';
import { handleGetRoom } from './handlers/getRoom';
import { handleGetAllRooms } from './handlers/getAllRooms';
import { handleGetBansForRoom } from './handlers/getBansForRoom';
import { handleIsUserBanned } from './handlers/isUserBanned';
import { handleGetAttachmentsForMessage } from './handlers/getAttachmentsForMessage';
import { handleGetAllAttachments } from './handlers/getAllAttachments';
import { handleGetPluginData } from './handlers/getPluginData';
import { handleGetMeta } from './handlers/getMeta';

export const buildQueryHandlers = (dataSource: DataSource): Record<QueryTypeKey, (query: Query) => Promise<unknown>> => ({
  [QueryType.GetMessages]:              (query) => handleGetMessages(query as GetMessagesQuery, dataSource),
  [QueryType.GetMessagesSince]:         (query) => handleGetMessagesSince(query as GetMessagesSinceQuery, dataSource),
  [QueryType.GetMessageById]:           (query) => handleGetMessageById(query as GetMessageByIdQuery, dataSource),
  [QueryType.GetReactionsForMessage]:   (query) => handleGetReactionsForMessage(query as GetReactionsForMessageQuery, dataSource),
  [QueryType.GetUser]:                  (query) => handleGetUser(query as GetUserQuery, dataSource),
  [QueryType.GetCurrentUser]:           () => handleGetCurrentUser(dataSource),
  [QueryType.GetCurrentUserId]:         () => handleGetCurrentUserId(dataSource),
  [QueryType.GetUsersByRoom]:           () => handleGetUsersByRoom(dataSource),
  [QueryType.GetRoom]:                  (query) => handleGetRoom(query as GetRoomQuery, dataSource),
  [QueryType.GetAllRooms]:              () => handleGetAllRooms(dataSource),
  [QueryType.GetBansForRoom]:           (query) => handleGetBansForRoom(query as GetBansForRoomQuery, dataSource),
  [QueryType.IsUserBanned]:             (query) => handleIsUserBanned(query as IsUserBannedQuery, dataSource),
  [QueryType.GetAttachmentsForMessage]: (query) => handleGetAttachmentsForMessage(query as GetAttachmentsForMessageQuery, dataSource),
  [QueryType.GetAllAttachments]:        () => handleGetAllAttachments(dataSource),
  [QueryType.GetPluginData]:            (query) => handleGetPluginData(query as GetPluginDataQuery, dataSource),
  [QueryType.GetMeta]:                  (query) => handleGetMeta(query as GetMetaQuery, dataSource)
});
