import { DataSource } from 'typeorm';
import {
  CommandType,
  CommandTypeKey,
  Command,
  RegisterUserCommand,
  UpsertServerCommand,
  DeleteServerCommand,
  CreateJoinRequestCommand,
  UpdateJoinRequestStatusCommand,
  DeleteStaleJoinRequestsCommand
} from '../types';
import { handleRegisterUser } from './handlers/registerUser';
import { handleUpsertServer } from './handlers/upsertServer';
import { handleDeleteServer } from './handlers/deleteServer';
import { handleCreateJoinRequest } from './handlers/createJoinRequest';
import { handleUpdateJoinRequestStatus } from './handlers/updateJoinRequestStatus';
import { handleDeleteStaleJoinRequests } from './handlers/deleteStaleJoinRequests';

export const buildCommandHandlers = (dataSource: DataSource): Record<CommandTypeKey, (command: Command) => Promise<unknown>> => ({
  [CommandType.RegisterUser]:            (cmd) => handleRegisterUser(cmd as RegisterUserCommand, dataSource),
  [CommandType.UpsertServer]:            (cmd) => handleUpsertServer(cmd as UpsertServerCommand, dataSource),
  [CommandType.DeleteServer]:            (cmd) => handleDeleteServer(cmd as DeleteServerCommand, dataSource),
  [CommandType.CreateJoinRequest]:       (cmd) => handleCreateJoinRequest(cmd as CreateJoinRequestCommand, dataSource),
  [CommandType.UpdateJoinRequestStatus]: (cmd) => handleUpdateJoinRequestStatus(cmd as UpdateJoinRequestStatusCommand, dataSource),
  [CommandType.DeleteStaleJoinRequests]: (cmd) => handleDeleteStaleJoinRequests(cmd as DeleteStaleJoinRequestsCommand, dataSource)
});
