import { DataSource } from 'typeorm';
import {
  ServerChannelPermissionEntity,
  ServerChannelEntity,
  ServerEntity,
  ServerRoleEntity,
  ServerTagEntity,
  ServerUserRoleEntity,
  JoinRequestEntity,
  ServerMembershipEntity,
  ServerInviteEntity,
  ServerBanEntity
} from '../../../entities';
import { DeleteServerCommand } from '../../types';

export async function handleDeleteServer(command: DeleteServerCommand, dataSource: DataSource): Promise<void> {
  const { serverId } = command.payload;

  await dataSource.transaction(async (manager) => {
    await manager.getRepository(ServerTagEntity).delete({ serverId });
    await manager.getRepository(ServerChannelEntity).delete({ serverId });
    await manager.getRepository(ServerRoleEntity).delete({ serverId });
    await manager.getRepository(ServerUserRoleEntity).delete({ serverId });
    await manager.getRepository(ServerChannelPermissionEntity).delete({ serverId });
    await manager.getRepository(JoinRequestEntity).delete({ serverId });
    await manager.getRepository(ServerMembershipEntity).delete({ serverId });
    await manager.getRepository(ServerInviteEntity).delete({ serverId });
    await manager.getRepository(ServerBanEntity).delete({ serverId });
    await manager.getRepository(ServerEntity).delete(serverId);
  });
}
