<commit_before>/*
 * Copyright 2017 HM Revenue & Customs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package controllers

import java.io.UnsupportedEncodingException
import java.net.URLDecoder

import com.github.nscala_time.time.OrderingImplicits.LocalDateOrdering
import common.FutureEx
import connectors.AssessmentScheduleExchangeObjects._
import connectors.ExchangeObjects.AllocationDetails
import connectors.{ CSREmailClient, EmailClient }
import model.AssessmentScheduleCommands.ApplicationForAssessmentAllocationResult
import model.AssessmentScheduleCommands.Implicits.ApplicationForAssessmentAllocationResultFormats
import model.Commands.Implicits.applicationAssessmentFormat
import model.Exceptions.NotFoundException
import model.{ Commands, ProgressStatuses }
import org.joda.time.LocalDate
import play.api.libs.json.{ JsValue, Json }
import play.api.mvc.{ Action, AnyContent, Result }
import repositories.AssessmentCentreLocation.assessmentCentreVenueFormat
import repositories._
import repositories.application._
import services.AuditService
import uk.gov.hmrc.play.microservice.controller.BaseController

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.util.Try

object AssessmentScheduleController extends AssessmentScheduleController {
  // val aaRepository: AssessmentCentreAllocationMongoRepository = assessmentCentreAllocationRepository
  val acRepository = AssessmentCentreYamlRepository
  val aRepository: GeneralApplicationMongoRepository = applicationRepository
  // val otRepository: OnlineTestMongoRepository = onlineTestRepository
  val auditService = AuditService
  // val pdRepository: PersonalDetailsRepository = personalDetailsRepository
  // val cdRepository: ContactDetailsRepository = contactDetailsRepository
  val emailClient = CSREmailClient
  // val aaService = AssessmentCentreService
  // val assessmentScoresService = AssessorAssessmentScoresService
}

trait AssessmentScheduleController extends BaseController {
  // val aaRepository: AssessmentCentreAllocationRepository
  val acRepository: AssessmentCentreRepository
  val aRepository: GeneralApplicationRepository
  // val otRepository: OnlineTestRepository
  val auditService: AuditService
  // val pdRepository: PersonalDetailsRepository
  // val cdRepository: ContactDetailsRepository
  val emailClient: EmailClient
  // val aaService: AssessmentCentreService
  // val assessmentScoresService: AssessmentCentreScoresService

  def getAssessmentScheduleDatesByRegion(region: String): Action[AnyContent] = Action.async { implicit request =>
     acRepository.assessmentCentreCapacities.map { schedule =>
       val toRet = schedule
         .filter(location => location.locationName == region)
         .flatMap(location =>
           location.venues.flatMap(venue =>
             venue.capacityDates.map(capacityDates =>
               capacityDates.date
             )
           )
         ).distinct.sorted
       Ok(Json.toJson(toRet))
     }
  }

  /*
  private def calculateUsedCapacity(assessments: Option[List[AssessmentCentreAllocation]], sessionCapacity: Int,
    minViableAttendees: Int, preferredAttendeeMargin: Int): UsedCapacity = {
    assessments.map { sessionSlots =>
      UsedCapacity(sessionSlots.size, sessionSlots.count(x => x.confirmed), minViableAttendees, preferredAttendeeMargin)
    }.getOrElse(
      UsedCapacity(usedCapacity = 0, confirmedAttendees = 0, minViableAttendees, preferredAttendeeMargin)
    )
  }

  def getAssessmentSchedule: Action[AnyContent] = Action.async { implicit request =>
    val assessments = aaRepository.findAll.map(_.groupBy(x => (x.venue, x.date, x.session)))<commit_msg>Review comments<commit_after>/*
 * Copyright 2017 HM Revenue & Customs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package controllers

import java.io.UnsupportedEncodingException
import java.net.URLDecoder

import com.github.nscala_time.time.OrderingImplicits.LocalDateOrdering
import common.FutureEx
import connectors.AssessmentScheduleExchangeObjects._
import connectors.ExchangeObjects.AllocationDetails
import connectors.{ CSREmailClient, EmailClient }
import model.AssessmentScheduleCommands.ApplicationForAssessmentAllocationResult
import model.AssessmentScheduleCommands.Implicits.ApplicationForAssessmentAllocationResultFormats
import model.Commands.Implicits.applicationAssessmentFormat
import model.Exceptions.NotFoundException
import model.{ Commands, ProgressStatuses }
import org.joda.time.LocalDate
import play.api.libs.json.{ JsValue, Json }
import play.api.mvc.{ Action, AnyContent, Result }
import repositories.AssessmentCentreLocation.assessmentCentreVenueFormat
import repositories._
import repositories.application._
import services.AuditService
import uk.gov.hmrc.play.microservice.controller.BaseController

import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
import scala.util.Try

object AssessmentScheduleController extends AssessmentScheduleController {
  // val aaRepository: AssessmentCentreAllocationMongoRepository = assessmentCentreAllocationRepository
  val acRepository = AssessmentCentreYamlRepository
  val aRepository: GeneralApplicationMongoRepository = applicationRepository
  // val otRepository: OnlineTestMongoRepository = onlineTestRepository
  val auditService = AuditService
  // val pdRepository: PersonalDetailsRepository = personalDetailsRepository
  // val cdRepository: ContactDetailsRepository = contactDetailsRepository
  val emailClient = CSREmailClient
  // val aaService = AssessmentCentreService
  // val assessmentScoresService = AssessorAssessmentScoresService
}

trait AssessmentScheduleController extends BaseController {
  // val aaRepository: AssessmentCentreAllocationRepository
  val acRepository: AssessmentCentreRepository
  val aRepository: GeneralApplicationRepository
  // val otRepository: OnlineTestRepository
  val auditService: AuditService
  // val pdRepository: PersonalDetailsRepository
  // val cdRepository: ContactDetailsRepository
  val emailClient: EmailClient
  // val aaService: AssessmentCentreService
  // val assessmentScoresService: AssessmentCentreScoresService

  def getAssessmentScheduleDatesByRegion(region: String): Action[AnyContent] = Action.async { implicit request =>
     acRepository.assessmentCentreCapacities.map { schedule =>
       val dates = schedule
         .filter(scheduleRegion => scheduleRegion.locationName == region)
         .flatMap(scheduleRegion =>
           scheduleRegion.venues.flatMap(venue =>
             venue.capacityDates.map(capacityDates =>
               capacityDates.date
             )
           )
         ).distinct.sorted
       Ok(Json.toJson(dates))
     }
  }

  /*
  private def calculateUsedCapacity(assessments: Option[List[AssessmentCentreAllocation]], sessionCapacity: Int,
    minViableAttendees: Int, preferredAttendeeMargin: Int): UsedCapacity = {
    assessments.map { sessionSlots =>
      UsedCapacity(sessionSlots.size, sessionSlots.count(x => x.confirmed), minViableAttendees, preferredAttendeeMargin)
    }.getOrElse(
      UsedCapacity(usedCapacity = 0, confirmedAttendees = 0, minViableAttendees, preferredAttendeeMargin)
    )
  }

  def getAssessmentSchedule: Action[AnyContent] = Action.async { implicit request =>
    val assessments = aaRepository.findAll.map(_.groupBy(x => (x.venue, x.date, x.session)))
{'fraction_non_alphanumeric': 0.0686623901164172, 'fraction_numerical': 0.0023758612497030173, 'mean_word_length': 5.083092485549133, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3220168', 'n_tokens_mistral': 2188, 'n_tokens_neox': 2105, 'n_words': 544}