// @flow
import http from 'http'
import PhenylRestApi from 'phenyl-rest-api'
import { createEntityClient } from 'phenyl-memory-db'
import { StandardUserDefinition, StandardEntityDefinition } from 'phenyl-standards'
import PhenylHttpServer from 'phenyl-http-server'
import PhenylApiExplorer from '../src/PhenylApiExplorer'
import type { Session, RequestData } from 'phenyl-interfaces'

const PORT = 8000
const memoryClient = createEntityClient()

class HospitalDefinition extends StandardEntityDefinition {
  async authorization(reqData: RequestData, session: ?Session): Promise<boolean> { // eslint-disable-line no-unused-vars
    return true
  }
}

class PatientDefinition extends StandardUserDefinition {
  constructor() {
    super({
      entityClient: memoryClient,
      accountPropName: 'email',
      passwordPropName: 'password',
      ttl: 24 * 3600
    })
  }

  async authorization(reqData, session): Promise<boolean> {
    const noLoginCommands = ['insertOne', 'insertAndGet', 'insertMulti', 'insertAndGetMulti', 'login']

    if (noLoginCommands.includes(reqData.method)) {
      return true
    }

    return session != null && session.userId === reqData.payload.id
  }
}

const functionalGroup = {
  customQueries: {

  },
  customCommands: {

  },
  users: {
    patient: new PatientDefinition(),
  },
  nonUsers: {
    hospital: new HospitalDefinition(),
  },
}

const server = new PhenylHttpServer(http.createServer(), {
  restApiHandler: PhenylRestApi.createFromFunctionalGroup(functionalGroup, {
    client: memoryClient,
  }),
  customRequestHandler: new PhenylApiExplorer(functionalGroup, { path: '/explorer' }).handler,
})

server.listen(PORT)
console.log(`server is listening on :${PORT}`)

@@ -933,160 +933,139 @@
     
-const noLoginCommands = %5B'insertOne', 'insertAndGet', 'insertMulti', 'insertAndGetMulti', 'login'%5D%0A%0A    if (noLoginCommands.includes(reqData.method)) %7B%0A
+switch (reqData.method) %7B%0A      case 'insertOne':%0A      case 'insertAndGet':%0A      case 'insertAndGetMulti':%0A      case 'login':%0A  
     
@@ -1082,19 +1082,31 @@
 rue%0A    
-%7D%0A%0A
+  default:%0A    
     retu
@@ -1127,49 +1127,14 @@
 null
- && session.userId === reqData.payload.id
+%0A    %7D
 %0A  %7D
@@ -1311,32 +1311,34 @@
 pitalDefinition(
+%7B%7D
 ),%0A  %7D,%0A%7D%0A%0Aconst

Fix flow errors in authorization