package org.brapi.test.BrAPITestServer.controller.geno;

import java.util.List;

import org.brapi.test.BrAPITestServer.controller.core.BrAPIController;
import org.brapi.test.BrAPITestServer.exceptions.BrAPIServerException;
import org.brapi.test.BrAPITestServer.model.dto.geno.ReferenceBasesWriteRequest;
import org.brapi.test.BrAPITestServer.model.dto.geno.ReferenceSetWriteRequest;
import org.brapi.test.BrAPITestServer.model.dto.geno.ReferenceWriteRequest;
import org.brapi.test.BrAPITestServer.service.geno.ReferenceBasesService;
import org.brapi.test.BrAPITestServer.service.geno.ReferenceService;
import org.brapi.test.BrAPITestServer.service.geno.ReferenceSetService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.model.Metadata;
import io.swagger.model.geno.Reference;
import io.swagger.model.geno.ReferenceBasesPage;
import io.swagger.model.geno.ReferenceBasesPageListResponse;
import io.swagger.model.geno.ReferenceBasesPageListResponseResult;
import io.swagger.model.geno.ReferenceBasesPageSingleResponse;
import io.swagger.model.geno.ReferenceSet;
import io.swagger.model.geno.ReferenceSetsListResponse;
import io.swagger.model.geno.ReferenceSetsListResponseResult;
import io.swagger.model.geno.ReferenceSetsSingleResponse;
import io.swagger.model.geno.ReferenceSingleResponse;
import io.swagger.model.geno.ReferencesListResponse;
import io.swagger.model.geno.ReferencesListResponseResult;
import jakarta.servlet.http.HttpServletRequest;

@RestController
public class GenotypingReferenceWriteController extends BrAPIController {

	private static final Logger log = LoggerFactory.getLogger(GenotypingReferenceWriteController.class);

	private final ReferenceSetService referenceSetService;
	private final ReferenceService referenceService;
	private final ReferenceBasesService referenceBasesService;
	private final HttpServletRequest request;

	@Autowired
	public GenotypingReferenceWriteController(ReferenceSetService referenceSetService, ReferenceService referenceService,
			ReferenceBasesService referenceBasesService, HttpServletRequest request) {
		this.referenceSetService = referenceSetService;
		this.referenceService = referenceService;
		this.referenceBasesService = referenceBasesService;
		this.request = request;
	}

	@CrossOrigin
	@RequestMapping(value = "/referencesets", produces = { "application/json" }, consumes = {
			"application/json" }, method = RequestMethod.POST)
	public ResponseEntity<ReferenceSetsListResponse> referenceSetsPost(@RequestBody ReferenceSetWriteRequest body,
			@RequestHeader(value = "Authorization", required = false) String authorization)
			throws BrAPIServerException {
		log.debug("Request: " + request.getRequestURI());
		validateSecurityContext(request, "ROLE_USER");
		validateAcceptHeader(request);
		ReferenceSet data = referenceSetService.saveReferenceSet(body);
		return responseOK(new ReferenceSetsListResponse(), new ReferenceSetsListResponseResult(), List.of(data));
	}

	@CrossOrigin
	@RequestMapping(value = "/referencesets/{referenceSetDbId}", produces = { "application/json" }, consumes = {
			"application/json" }, method = RequestMethod.PUT)
	public ResponseEntity<ReferenceSetsSingleResponse> referenceSetsReferenceSetDbIdPut(
			@PathVariable("referenceSetDbId") String referenceSetDbId, @RequestBody ReferenceSetWriteRequest body,
			@RequestHeader(value = "Authorization", required = false) String authorization)
			throws BrAPIServerException {
		log.debug("Request: " + request.getRequestURI());
		validateSecurityContext(request, "ROLE_USER");
		validateAcceptHeader(request);
		ReferenceSet data = referenceSetService.updateReferenceSet(referenceSetDbId, body);
		return responseOK(new ReferenceSetsSingleResponse(), data);
	}

	@CrossOrigin
	@RequestMapping(value = "/referencesets/{referenceSetDbId}", produces = {
			"application/json" }, method = RequestMethod.DELETE)
	public ResponseEntity<ReferenceSetsSingleResponse> referenceSetsReferenceSetDbIdDelete(
			@PathVariable("referenceSetDbId") String referenceSetDbId,
			@RequestHeader(value = "Authorization", required = false) String authorization)
			throws BrAPIServerException {
		log.debug("Request: " + request.getRequestURI());
		validateSecurityContext(request, "ROLE_USER");
		validateAcceptHeader(request);
		ReferenceSet data = referenceSetService.deleteReferenceSet(referenceSetDbId);
		return responseOK(new ReferenceSetsSingleResponse(), data);
	}

	@CrossOrigin
	@RequestMapping(value = "/references", produces = { "application/json" }, consumes = {
			"application/json" }, method = RequestMethod.POST)
	public ResponseEntity<ReferencesListResponse> referencesPost(@RequestBody ReferenceWriteRequest body,
			@RequestHeader(value = "Authorization", required = false) String authorization)
			throws BrAPIServerException {
		log.debug("Request: " + request.getRequestURI());
		validateSecurityContext(request, "ROLE_USER");
		validateAcceptHeader(request);
		Reference data = referenceService.saveReference(body);
		return responseOK(new ReferencesListResponse(), new ReferencesListResponseResult(), List.of(data));
	}

	@CrossOrigin
	@RequestMapping(value = "/references/{referenceDbId}", produces = { "application/json" }, consumes = {
			"application/json" }, method = RequestMethod.PUT)
	public ResponseEntity<ReferenceSingleResponse> referencesReferenceDbIdPut(
			@PathVariable("referenceDbId") String referenceDbId, @RequestBody ReferenceWriteRequest body,
			@RequestHeader(value = "Authorization", required = false) String authorization)
			throws BrAPIServerException {
		log.debug("Request: " + request.getRequestURI());
		validateSecurityContext(request, "ROLE_USER");
		validateAcceptHeader(request);
		Reference data = referenceService.updateReference(referenceDbId, body);
		return responseOK(new ReferenceSingleResponse(), data);
	}

	@CrossOrigin
	@RequestMapping(value = "/references/{referenceDbId}", produces = {
			"application/json" }, method = RequestMethod.DELETE)
	public ResponseEntity<ReferenceSingleResponse> referencesReferenceDbIdDelete(
			@PathVariable("referenceDbId") String referenceDbId,
			@RequestHeader(value = "Authorization", required = false) String authorization)
			throws BrAPIServerException {
		log.debug("Request: " + request.getRequestURI());
		validateSecurityContext(request, "ROLE_USER");
		validateAcceptHeader(request);
		Reference data = referenceService.deleteReference(referenceDbId);
		return responseOK(new ReferenceSingleResponse(), data);
	}

	@CrossOrigin
	@RequestMapping(value = "/referencebases", produces = { "application/json" }, method = RequestMethod.GET)
	public ResponseEntity<ReferenceBasesPageListResponse> referenceBasesGet(
			@RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestHeader(value = "Authorization", required = false) String authorization)
			throws BrAPIServerException {
		log.debug("Request: " + request.getRequestURI());
		validateSecurityContext(request, "ROLE_ANONYMOUS", "ROLE_USER");
		validateAcceptHeader(request);
		Metadata metadata = generateMetaDataTemplate(page, pageSize);
		List<ReferenceBasesPage> data = referenceBasesService.findReferenceBasesPages(metadata);
		return responseOK(new ReferenceBasesPageListResponse(), new ReferenceBasesPageListResponseResult(), data,
				metadata);
	}

	@CrossOrigin
	@RequestMapping(value = "/referencebases", produces = { "application/json" }, consumes = {
			"application/json" }, method = RequestMethod.POST)
	public ResponseEntity<ReferenceBasesPageListResponse> referenceBasesPost(@RequestBody ReferenceBasesWriteRequest body,
			@RequestHeader(value = "Authorization", required = false) String authorization)
			throws BrAPIServerException {
		log.debug("Request: " + request.getRequestURI());
		validateSecurityContext(request, "ROLE_USER");
		validateAcceptHeader(request);
		ReferenceBasesPage data = referenceBasesService.saveReferenceBasesPage(body);
		return responseOK(new ReferenceBasesPageListResponse(), new ReferenceBasesPageListResponseResult(), List.of(data));
	}

	@CrossOrigin
	@RequestMapping(value = "/referencebases/{referenceBasesDbId}", produces = { "application/json" }, consumes = {
			"application/json" }, method = RequestMethod.PUT)
	public ResponseEntity<ReferenceBasesPageSingleResponse> referenceBasesReferenceBasesDbIdPut(
			@PathVariable("referenceBasesDbId") String referenceBasesDbId, @RequestBody ReferenceBasesWriteRequest body,
			@RequestHeader(value = "Authorization", required = false) String authorization)
			throws BrAPIServerException {
		log.debug("Request: " + request.getRequestURI());
		validateSecurityContext(request, "ROLE_USER");
		validateAcceptHeader(request);
		ReferenceBasesPage data = referenceBasesService.updateReferenceBasesPage(referenceBasesDbId, body);
		return responseOK(new ReferenceBasesPageSingleResponse(), data);
	}

	@CrossOrigin
	@RequestMapping(value = "/referencebases/{referenceBasesDbId}", produces = {
			"application/json" }, method = RequestMethod.DELETE)
	public ResponseEntity<ReferenceBasesPageSingleResponse> referenceBasesReferenceBasesDbIdDelete(
			@PathVariable("referenceBasesDbId") String referenceBasesDbId,
			@RequestHeader(value = "Authorization", required = false) String authorization)
			throws BrAPIServerException {
		log.debug("Request: " + request.getRequestURI());
		validateSecurityContext(request, "ROLE_USER");
		validateAcceptHeader(request);
		ReferenceBasesPage data = referenceBasesService.deleteReferenceBasesPage(referenceBasesDbId);
		return responseOK(new ReferenceBasesPageSingleResponse(), data);
	}
}
