package ru.micord.ervu.account_applications.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import ru.micord.ervu.account_applications.security.context.SecurityContext;
import ru.micord.ervu.account_applications.service.RecruitmentService;

/**
 * @author gulnaz
 */
@Controller
public class RecruitmentController {

  private final SecurityContext securityContext;
  private final RecruitmentService recruitmentService;

  public RecruitmentController(SecurityContext securityContext, RecruitmentService recruitmentService) {
    this.securityContext = securityContext;
    this.recruitmentService = recruitmentService;
  }

  @GetMapping(value = "/allowed")
  public ResponseEntity<Boolean> checkAccess(HttpServletRequest request) {
    Long appNumber = Long.valueOf(request.getHeader("app-number"));
    boolean checkParents = Boolean.parseBoolean(request.getHeader("check-parents"));
    String domainId = securityContext.getDomainId();
    return ResponseEntity.ok(recruitmentService.exists(appNumber, domainId, checkParents));
  }
}
