import { join } from 'path';
import { cpy, copy, mkdirs, replaceCode, addNpmPackage } from '../utils';

async function generateCommonAuthenticationExpress(params) {
  const build = join(__base, 'build', params.uuid);
  const app = join(build, 'app.js');
  const passportConfigFile = join(build, 'config', 'passport.js');
  const passportConfigModule = join(__dirname, 'modules', 'common', 'passport-config.js');
  const passportConfigRequire = join(__dirname, 'modules', 'common', 'passport-config-require.js');
  const passportCommonRoutes = join(__dirname, 'modules', 'common', 'passport-routes.js');
  const passportRequire = join(__dirname, 'modules', 'common', 'passport-require.js');
  const passportMiddleware = join(__dirname, 'modules', 'common', 'passport-middleware.js');
  const passportSerializer = join(__dirname, 'modules', 'common', 'passport-serializer.js');
  const passportDeserializerMongoDb = join(__dirname, 'modules', 'common', 'passport-deserializer.js');
  const passportDeserializerSql = join(__dirname, 'modules', 'common', 'passport-deserializer-sql.js');
  const passportUserModel = join(__dirname, 'modules', 'common', 'passport-user-model.js');
  const userControllerModule = join(__dirname, 'modules', 'controllers', 'user.js');
  const userControllerRequire = join(__dirname, 'modules', 'controllers', 'user-require.js');
  const accountRoutes = join(__dirname, 'modules', 'common', 'routes', 'account-routes.js');

  // Passport middleware
  await replaceCode(app, 'PASSPORT_REQUIRE', passportRequire);
  await replaceCode(app, 'PASSPORT_MIDDLEWARE', passportMiddleware);
  await replaceCode(app, 'PASSPORT_CONFIG_REQUIRE', passportConfigRequire);

  // Add user controller reference
  await replaceCode(app, 'USER_CONTROLLER', userControllerRequire);

  // Add common Passport routes, e.g. logout, unlink
  await replaceCode(app, 'PASSPORT_COMMON_ROUTES', passportCommonRoutes);

  // Add "My Account" routes
  await replaceCode(app, 'ACCOUNT_ROUTES', accountRoutes);

  // Passport config file
  await copy(passportConfigModule, passportConfigFile);
  await replaceCode(passportConfigFile, 'PASSPORT_USER_MODEL', passportUserModel);
  await replaceCode(passportConfigFile, 'PASSPORT_SERIALIZER', passportSerializer);

  await addNpmPackage('passport', params);

  // Copy user controller (replace database-specific things below)
  await copy(userControllerModule, join(build, 'controllers', 'user.js'));
  const userController = join(build, 'controllers', 'user.js');

  switch (params.database) {
    case 'mongodb':
      const mongooseModel = join(__dirname, 'modules', 'models', 'mongodb', 'user.js');
      const userHelperMiddlewareForMongoDb = join(__dirname, 'modules', 'common', 'user-middleware-mongodb.js');

      await copy(mongooseModel, join(build, 'models', 'user.js'));

      await replaceCode(app, 'USER_HELPER_MIDDLEWARE', userHelperMiddlewareForMongoDb);

      await replaceCode(passportConfigFile, 'PASSPORT_DESERIALIZER', passportDeserializerMongoDb);

      await replaceCode(userController, 'USER_SIGNUP_POST', join(__dirname, 'modules', 'controllers', 'mongodb', 'user-signup-post.js'), { indentLevel: 1 });
      await replaceCode(userController, 'USER_ACCOUNT_PUT', join(__dirname, 'modules', 'controllers', 'mongodb', 'user-account-put.js'), { indentLevel: 1 });
      await replaceCode(userController, 'USER_ACCOUNT_DELETE', join(__dirname, 'modules', 'controllers', 'mongodb', 'user-account-delete.js'), { indentLevel: 1 });
      await replaceCode(userController, 'USER_PROVIDER_UNLINK', join(__dirname, 'modules', 'controllers', 'mongodb', 'user-provider-unlink.js'), { indentLevel: 1 });
      await replaceCode(userController, 'USER_FORGOT_POST', join(__dirname, 'modules', 'controllers', 'mongodb', 'user-forgot-post.js'), { indentLevel: 3 });
      await replaceCode(userController, 'USER_RESET_GET', join(__dirname, 'modules', 'controllers', 'mongodb', 'user-reset-get.js'), { indentLevel: 1 });
      await replaceCode(userController, 'USER_RESET_POST', join(__dirname, 'modules', 'controllers', 'mongodb', 'user-reset-post.js'), { indentLevel: 3 });
      break;

    case 'mysql':
    case 'sqlite':
    case 'postgresql':
      const bookshelfModel = join(__dirname, 'modules', 'models', 'sql', 'user.js');
      const userHelperMiddlewareForSql = join(__dirname, 'modules', 'common', 'user-middleware-sql.js');

      await copy(bookshelfModel, join(build, 'models', 'user.js'));
      await copy(join(__dirname, 'modules', 'migrations'), join(build, 'migrations'));

      await replaceCode(app, 'USER_HELPER_MIDDLEWARE', userHelperMiddlewareForSql);


      await replaceCode(passportConfigFile, 'PASSPORT_DESERIALIZER', passportDeserializerSql);

      await replaceCode(userController, 'USER_SIGNUP_POST', join(__dirname, 'modules', 'controllers', 'sql', 'user-signup-post.js'), { indentLevel: 1 });
      await replaceCode(userController, 'USER_ACCOUNT_PUT', join(__dirname, 'modules', 'controllers', 'sql', 'user-account-put.js'), { indentLevel: 1 });
      await replaceCode(userController, 'USER_ACCOUNT_DELETE', join(__dirname, 'modules', 'controllers', 'sql', 'user-account-delete.js'), { indentLevel: 1 });
      await replaceCode(userController, 'USER_PROVIDER_UNLINK', join(__dirname, 'modules', 'controllers', 'sql', 'user-provider-unlink.js'), { indentLevel: 1 });
      await replaceCode(userController, 'USER_FORGOT_POST', join(__dirname, 'modules', 'controllers', 'sql', 'user-forgot-post.js'), { indentLevel: 3 });
      await replaceCode(userController, 'USER_RESET_GET', join(__dirname, 'modules', 'controllers', 'sql', 'user-reset-get.js'), { indentLevel: 1 });
      await replaceCode(userController, 'USER_RESET_POST', join(__dirname, 'modules', 'controllers', 'sql', 'user-reset-post.js'), { indentLevel: 3 });
      break;

    case 'rethinkdb':
      break;

    default:
      break;
  }

  switch (params.templateEngine) {
    case 'jade':
      const loginJade = join(__dirname, 'modules', 'common', 'views', 'login.jade');
      const signupJade = join(__dirname, 'modules', 'common', 'views', 'signup.jade');
      const forgotJade = join(__dirname, 'modules', 'common', 'views', 'forgot.jade');
      const resetJade = join(__dirname, 'modules', 'common', 'views', 'reset.jade');
      const profileJade = join(__dirname, 'modules', 'common', 'views', 'profile.jade');
      await mkdirs(join(build, 'views', 'account'));
      await cpy([
        loginJade,
        signupJade,
        forgotJade,
        resetJade,
        profileJade
      ], join(build, 'views', 'account'));
      break;
    default:
      break;
  }
}

export default generateCommonAuthenticationExpress;

@@ -49,16 +49,28 @@
  mkdirs,
+ appendFile,
  replace
@@ -255,24 +255,59 @@
  'app.js');%0A
+  const env = join(build, '.env');%0A
   const pass
@@ -6676,16 +6676,137 @@
 eak;%0A  %7D
+%0A%0A  if (params.jsFramework) %7B%0A    await appendFile(env, '%5CnTOKEN_SECRET=Secret key for signing and verifying JWT%5Cn');%0A  %7D
 %0A%7D%0A%0Aexpo

Append token_secret env var if js framework is selected