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.CallWriteRequest;
import org.brapi.test.BrAPITestServer.service.geno.CallService;
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.Metadata;
import io.swagger.model.geno.Call;
import io.swagger.model.geno.CallsListResponse;
import io.swagger.model.geno.CallsListResponseResult;
import jakarta.servlet.http.HttpServletRequest;

@RestController
public class GenotypingCallWriteController extends BrAPIController {

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

	private final CallService callService;
	private final HttpServletRequest request;

	@Autowired
	public GenotypingCallWriteController(CallService callService, HttpServletRequest request) {
		this.callService = callService;
		this.request = request;
	}

	@CrossOrigin
	@RequestMapping(value = "/calls", produces = { "application/json" }, consumes = {
			"application/json" }, method = RequestMethod.POST)
	public ResponseEntity<CallsListResponse> callsPost(@RequestBody CallWriteRequest body,
			@RequestHeader(value = "Authorization", required = false) String authorization)
			throws BrAPIServerException {
		log.debug("Request: " + request.getRequestURI());
		validateSecurityContext(request, "ROLE_USER");
		validateAcceptHeader(request);
		Call data = callService.saveCall(body);
		Metadata metadata = generateEmptyMetadata();
		CallsListResponseResult result = new CallsListResponseResult();
		result.setData(List.of(data));
		return responseOK(new CallsListResponse(), result, metadata);
	}

	@CrossOrigin
	@RequestMapping(value = "/calls/import", produces = { "application/json" }, consumes = {
			"application/json" }, method = RequestMethod.POST)
	public ResponseEntity<CallsListResponse> callsImportPost(@RequestBody List<CallWriteRequest> body,
			@RequestHeader(value = "Authorization", required = false) String authorization)
			throws BrAPIServerException {
		log.debug("Request: " + request.getRequestURI());
		validateSecurityContext(request, "ROLE_USER");
		validateAcceptHeader(request);
		List<Call> data = callService.importCalls(body);
		Metadata metadata = generateEmptyMetadata();
		CallsListResponseResult result = new CallsListResponseResult();
		result.setData(data);
		return responseOK(new CallsListResponse(), result, metadata);
	}

	@CrossOrigin
	@RequestMapping(value = "/calls/{callDbId}", produces = { "application/json" }, consumes = {
			"application/json" }, method = RequestMethod.PUT)
	public ResponseEntity<CallsListResponse> callsCallDbIdPut(@PathVariable("callDbId") String callDbId,
			@RequestBody CallWriteRequest body,
			@RequestHeader(value = "Authorization", required = false) String authorization)
			throws BrAPIServerException {
		log.debug("Request: " + request.getRequestURI());
		validateSecurityContext(request, "ROLE_USER");
		validateAcceptHeader(request);
		Call data = callService.updateCall(callDbId, body);
		Metadata metadata = generateEmptyMetadata();
		CallsListResponseResult result = new CallsListResponseResult();
		result.setData(List.of(data));
		return responseOK(new CallsListResponse(), result, metadata);
	}

	@CrossOrigin
	@RequestMapping(value = "/calls/{callDbId}", produces = {
			"application/json" }, method = RequestMethod.DELETE)
	public ResponseEntity<CallsListResponse> callsCallDbIdDelete(@PathVariable("callDbId") String callDbId,
			@RequestHeader(value = "Authorization", required = false) String authorization)
			throws BrAPIServerException {
		log.debug("Request: " + request.getRequestURI());
		validateSecurityContext(request, "ROLE_USER");
		validateAcceptHeader(request);
		Call data = callService.deleteCall(callDbId);
		Metadata metadata = generateEmptyMetadata();
		CallsListResponseResult result = new CallsListResponseResult();
		result.setData(List.of(data));
		return responseOK(new CallsListResponse(), result, metadata);
	}
}
