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.CallSetWriteRequest;
import org.brapi.test.BrAPITestServer.service.geno.CallSetService;
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.RestController;

import io.swagger.model.geno.CallSet;
import io.swagger.model.geno.CallSetResponse;
import io.swagger.model.geno.CallSetsListResponse;
import io.swagger.model.geno.CallSetsListResponseResult;
import jakarta.servlet.http.HttpServletRequest;

@RestController
public class GenotypingCallSetWriteController extends BrAPIController {

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

	private final CallSetService callSetService;
	private final HttpServletRequest request;

	@Autowired
	public GenotypingCallSetWriteController(CallSetService callSetService, HttpServletRequest request) {
		this.callSetService = callSetService;
		this.request = request;
	}

	@CrossOrigin
	@RequestMapping(value = "/callsets", produces = { "application/json" }, consumes = {
			"application/json" }, method = RequestMethod.POST)
	public ResponseEntity<CallSetsListResponse> callSetsPost(@RequestBody CallSetWriteRequest body,
			@RequestHeader(value = "Authorization", required = false) String authorization)
			throws BrAPIServerException {
		log.debug("Request: " + request.getRequestURI());
		validateSecurityContext(request, "ROLE_USER");
		validateAcceptHeader(request);
		CallSet data = callSetService.saveCallSet(body);
		return responseOK(new CallSetsListResponse(), new CallSetsListResponseResult(), List.of(data));
	}

	@CrossOrigin
	@RequestMapping(value = "/callsets/{callSetDbId}", produces = { "application/json" }, consumes = {
			"application/json" }, method = RequestMethod.PUT)
	public ResponseEntity<CallSetResponse> callSetsCallSetDbIdPut(@PathVariable("callSetDbId") String callSetDbId,
			@RequestBody CallSetWriteRequest body,
			@RequestHeader(value = "Authorization", required = false) String authorization)
			throws BrAPIServerException {
		log.debug("Request: " + request.getRequestURI());
		validateSecurityContext(request, "ROLE_USER");
		validateAcceptHeader(request);
		CallSet data = callSetService.updateCallSet(callSetDbId, body);
		return responseOK(new CallSetResponse(), data);
	}

	@CrossOrigin
	@RequestMapping(value = "/callsets/{callSetDbId}", produces = {
			"application/json" }, method = RequestMethod.DELETE)
	public ResponseEntity<CallSetResponse> callSetsCallSetDbIdDelete(@PathVariable("callSetDbId") String callSetDbId,
			@RequestHeader(value = "Authorization", required = false) String authorization)
			throws BrAPIServerException {
		log.debug("Request: " + request.getRequestURI());
		validateSecurityContext(request, "ROLE_USER");
		validateAcceptHeader(request);
		CallSet data = callSetService.deleteCallSet(callSetDbId);
		return responseOK(new CallSetResponse(), data);
	}
}
