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

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.brapi.test.BrAPITestServer.controller.core.BrAPIController;
import org.brapi.test.BrAPITestServer.exceptions.BrAPIServerException;
import org.brapi.test.BrAPITestServer.model.dto.germ.GermplasmInstituteRecord;
import org.brapi.test.BrAPITestServer.model.dto.germ.GermplasmInstituteWriteRequest;
import org.brapi.test.BrAPITestServer.service.germ.GermplasmService;
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 jakarta.servlet.http.HttpServletRequest;

@RestController
public class GermplasmProfileWriteController extends BrAPIController {

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

	private final GermplasmService germplasmService;
	private final HttpServletRequest request;

	@Autowired
	public GermplasmProfileWriteController(GermplasmService germplasmService, HttpServletRequest request) {
		this.germplasmService = germplasmService;
		this.request = request;
	}

	@CrossOrigin
	@RequestMapping(value = "/germplasm/{germplasmDbId}/institutes", produces = {
			"application/json" }, method = RequestMethod.GET)
	public ResponseEntity<Map<String, Object>> germplasmGermplasmDbIdInstitutesGet(
			@PathVariable("germplasmDbId") String germplasmDbId,
			@RequestHeader(value = "Authorization", required = false) String authorization)
			throws BrAPIServerException {
		log.debug("Request: " + request.getRequestURI());
		validateSecurityContext(request, "ROLE_ANONYMOUS", "ROLE_USER");
		validateAcceptHeader(request);
		List<GermplasmInstituteRecord> data = germplasmService.findInstitutes(germplasmDbId);
		Map<String, Object> result = new HashMap<>();
		result.put("data", data);
		return ResponseEntity.ok(wrapOk(result));
	}

	@CrossOrigin
	@RequestMapping(value = "/germplasm/{germplasmDbId}/institutes", produces = {
			"application/json" }, consumes = { "application/json" }, method = RequestMethod.POST)
	public ResponseEntity<Map<String, Object>> germplasmGermplasmDbIdInstitutesPost(
			@PathVariable("germplasmDbId") String germplasmDbId,
			@RequestBody GermplasmInstituteWriteRequest body,
			@RequestHeader(value = "Authorization", required = false) String authorization)
			throws BrAPIServerException {
		log.debug("Request: " + request.getRequestURI());
		validateSecurityContext(request, "ROLE_USER");
		validateAcceptHeader(request);
		GermplasmInstituteRecord data = germplasmService.saveInstitute(germplasmDbId, body);
		Map<String, Object> result = new HashMap<>();
		result.put("data", List.of(data));
		return ResponseEntity.ok(wrapOk(result));
	}

	@CrossOrigin
	@RequestMapping(value = "/germplasm/{germplasmDbId}/institutes/{instituteDbId}", produces = {
			"application/json" }, consumes = { "application/json" }, method = RequestMethod.PUT)
	public ResponseEntity<Map<String, Object>> germplasmGermplasmDbIdInstitutesInstituteDbIdPut(
			@PathVariable("germplasmDbId") String germplasmDbId,
			@PathVariable("instituteDbId") String instituteDbId,
			@RequestBody GermplasmInstituteWriteRequest body,
			@RequestHeader(value = "Authorization", required = false) String authorization)
			throws BrAPIServerException {
		log.debug("Request: " + request.getRequestURI());
		validateSecurityContext(request, "ROLE_USER");
		validateAcceptHeader(request);
		GermplasmInstituteRecord data = germplasmService.updateInstitute(germplasmDbId, instituteDbId, body);
		Map<String, Object> result = new HashMap<>();
		result.put("data", List.of(data));
		return ResponseEntity.ok(wrapOk(result));
	}

	@CrossOrigin
	@RequestMapping(value = "/germplasm/{germplasmDbId}/institutes/{instituteDbId}", produces = {
			"application/json" }, method = RequestMethod.DELETE)
	public ResponseEntity<Map<String, Object>> germplasmGermplasmDbIdInstitutesInstituteDbIdDelete(
			@PathVariable("germplasmDbId") String germplasmDbId,
			@PathVariable("instituteDbId") String instituteDbId,
			@RequestHeader(value = "Authorization", required = false) String authorization)
			throws BrAPIServerException {
		log.debug("Request: " + request.getRequestURI());
		validateSecurityContext(request, "ROLE_USER");
		validateAcceptHeader(request);
		GermplasmInstituteRecord data = germplasmService.deleteInstitute(germplasmDbId, instituteDbId);
		Map<String, Object> result = new HashMap<>();
		result.put("data", List.of(data));
		return ResponseEntity.ok(wrapOk(result));
	}

	private Map<String, Object> wrapOk(Map<String, Object> result) {
		Map<String, Object> response = new HashMap<>();
		response.put("metadata", generateEmptyMetadata());
		response.put("result", result);
		return response;
	}

}
