package code.api.sandbox

import code.api.{APIFailure, OBPRestHelper}
import code.api.util.APIUtil._
import code.api.util.ErrorMessages
import code.sandbox.{OBPDataImport, SandboxDataImport}
import code.util.Helper
import net.liftweb.http.S
import net.liftweb.http.js.JE.JsRaw
import net.liftweb.http.rest.RestHelper
import net.liftweb.util.Helpers._
import net.liftweb.util.Props
import code.util.Helper.MdcLoggable


object SandboxApiCalls extends OBPRestHelper with MdcLoggable {

  //needs to be a RestHelper to get access to JsonGet, JsonPost, etc.
  self: RestHelper =>
  logger.debug("Hello from SandboxApiCalls")
  val version = "sandbox"
  val versionStatus = "DEPRECIATED"

  oauthServe(apiPrefix{

    case "v1.0" :: "data-import" :: Nil JsonPost json -> _ => {
      user =>
        logger.info("Hello from v1.0 data-import")
        for{
          correctToken <- Props.get("sandbox_data_import_secret") ~> APIFailure("Data import is disabled for this API instance.", 403)
          providedToken <- S.param("secret_token") ~> APIFailure("secret_token parameter required", 403)
          tokensMatch <- Helper.booleanToBox(providedToken == correctToken) ~> APIFailure("incorrect secret token", 403)
          importData <- tryo{json.extract[SandboxDataImport]} ?~ ErrorMessages.InvalidJsonFormat
          importWorked <- OBPDataImport.importer.vend.importData(importData)
        } yield {
          successJsonResponse(JsRaw("{}"), 201)
        }

    }

  })


}
