import { DataSource } from 'typeorm';
import {
  QueryType,
  QueryTypeKey,
  Query,
  GetUserByUsernameQuery,
  GetUserByIdQuery,
  GetServerByIdQuery,
  GetJoinRequestByIdQuery,
  GetPendingRequestsForServerQuery
} from '../types';
import { handleGetUserByUsername } from './handlers/getUserByUsername';
import { handleGetUserById } from './handlers/getUserById';
import { handleGetAllPublicServers } from './handlers/getAllPublicServers';
import { handleGetServerById } from './handlers/getServerById';
import { handleGetJoinRequestById } from './handlers/getJoinRequestById';
import { handleGetPendingRequestsForServer } from './handlers/getPendingRequestsForServer';

export const buildQueryHandlers = (dataSource: DataSource): Record<QueryTypeKey, (query: Query) => Promise<unknown>> => ({
  [QueryType.GetUserByUsername]:           (query) => handleGetUserByUsername(query as GetUserByUsernameQuery, dataSource),
  [QueryType.GetUserById]:                 (query) => handleGetUserById(query as GetUserByIdQuery, dataSource),
  [QueryType.GetAllPublicServers]:         () => handleGetAllPublicServers(dataSource),
  [QueryType.GetServerById]:               (query) => handleGetServerById(query as GetServerByIdQuery, dataSource),
  [QueryType.GetJoinRequestById]:          (query) => handleGetJoinRequestById(query as GetJoinRequestByIdQuery, dataSource),
  [QueryType.GetPendingRequestsForServer]: (query) => handleGetPendingRequestsForServer(query as GetPendingRequestsForServerQuery, dataSource)
});
