import { DataSource } from 'typeorm';
import {
  CommandType,
  CommandTypeKey,
  Command,
  SaveMessageCommand,
  DeleteMessageCommand,
  UpdateMessageCommand,
  ClearRoomMessagesCommand,
  SaveReactionCommand,
  RemoveReactionCommand,
  SaveUserCommand,
  SetCurrentUserIdCommand,
  UpdateUserCommand,
  SaveRoomCommand,
  DeleteRoomCommand,
  UpdateRoomCommand,
  SaveBanCommand,
  RemoveBanCommand,
  SaveAttachmentCommand,
  DeleteAttachmentsForMessageCommand,
  SavePluginDataCommand,
  DeletePluginDataCommand,
  SaveMetaCommand
} from '../types';
import { handleSaveMessage } from './handlers/saveMessage';
import { handleDeleteMessage } from './handlers/deleteMessage';
import { handleUpdateMessage } from './handlers/updateMessage';
import { handleClearRoomMessages } from './handlers/clearRoomMessages';
import { handleSaveReaction } from './handlers/saveReaction';
import { handleRemoveReaction } from './handlers/removeReaction';
import { handleSaveUser } from './handlers/saveUser';
import { handleSetCurrentUserId } from './handlers/setCurrentUserId';
import { handleUpdateUser } from './handlers/updateUser';
import { handleSaveRoom } from './handlers/saveRoom';
import { handleDeleteRoom } from './handlers/deleteRoom';
import { handleUpdateRoom } from './handlers/updateRoom';
import { handleSaveBan } from './handlers/saveBan';
import { handleRemoveBan } from './handlers/removeBan';
import { handleSaveAttachment } from './handlers/saveAttachment';
import { handleDeleteAttachmentsForMessage } from './handlers/deleteAttachmentsForMessage';
import { handleSavePluginData } from './handlers/savePluginData';
import { handleDeletePluginData } from './handlers/deletePluginData';
import { handleSaveMeta } from './handlers/saveMeta';
import { handleClearAllData } from './handlers/clearAllData';

export const buildCommandHandlers = (dataSource: DataSource): Record<CommandTypeKey, (command: Command) => Promise<unknown>> => ({
  [CommandType.SaveMessage]: (cmd) => handleSaveMessage(cmd as SaveMessageCommand, dataSource),
  [CommandType.DeleteMessage]: (cmd) => handleDeleteMessage(cmd as DeleteMessageCommand, dataSource),
  [CommandType.UpdateMessage]: (cmd) => handleUpdateMessage(cmd as UpdateMessageCommand, dataSource),
  [CommandType.ClearRoomMessages]: (cmd) => handleClearRoomMessages(cmd as ClearRoomMessagesCommand, dataSource),
  [CommandType.SaveReaction]: (cmd) => handleSaveReaction(cmd as SaveReactionCommand, dataSource),
  [CommandType.RemoveReaction]: (cmd) => handleRemoveReaction(cmd as RemoveReactionCommand, dataSource),
  [CommandType.SaveUser]: (cmd) => handleSaveUser(cmd as SaveUserCommand, dataSource),
  [CommandType.SetCurrentUserId]: (cmd) => handleSetCurrentUserId(cmd as SetCurrentUserIdCommand, dataSource),
  [CommandType.UpdateUser]: (cmd) => handleUpdateUser(cmd as UpdateUserCommand, dataSource),
  [CommandType.SaveRoom]: (cmd) => handleSaveRoom(cmd as SaveRoomCommand, dataSource),
  [CommandType.DeleteRoom]: (cmd) => handleDeleteRoom(cmd as DeleteRoomCommand, dataSource),
  [CommandType.UpdateRoom]: (cmd) => handleUpdateRoom(cmd as UpdateRoomCommand, dataSource),
  [CommandType.SaveBan]: (cmd) => handleSaveBan(cmd as SaveBanCommand, dataSource),
  [CommandType.RemoveBan]: (cmd) => handleRemoveBan(cmd as RemoveBanCommand, dataSource),
  [CommandType.SaveAttachment]: (cmd) => handleSaveAttachment(cmd as SaveAttachmentCommand, dataSource),
  [CommandType.DeleteAttachmentsForMessage]: (cmd) => handleDeleteAttachmentsForMessage(cmd as DeleteAttachmentsForMessageCommand, dataSource),
  [CommandType.SavePluginData]: (cmd) => handleSavePluginData(cmd as SavePluginDataCommand, dataSource),
  [CommandType.DeletePluginData]: (cmd) => handleDeletePluginData(cmd as DeletePluginDataCommand, dataSource),
  [CommandType.SaveMeta]: (cmd) => handleSaveMeta(cmd as SaveMetaCommand, dataSource),
  [CommandType.ClearAllData]: () => handleClearAllData(dataSource)
});
