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

import io.swagger.model.Metadata;
import io.swagger.model.core.CommonCrop;
import io.swagger.model.core.CommonCropListResponse;
import io.swagger.model.core.CommonCropListResponseResult;
import io.swagger.model.core.CommonCropNamesResponse;
import io.swagger.model.core.CommonCropNamesResponseResult;
import io.swagger.model.core.CommonCropNewRequest;
import io.swagger.api.core.CommonCropNamesApi;

import org.brapi.test.BrAPITestServer.exceptions.BrAPIServerException;
import org.brapi.test.BrAPITestServer.service.core.CropService;
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.RestController;
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.RequestParam;

import jakarta.validation.Valid;
import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class CommonCropNamesApiController extends BrAPIController implements CommonCropNamesApi {

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

	private final CropService cropService;
	private final HttpServletRequest request;

	@Autowired
	public CommonCropNamesApiController(CropService cropService, HttpServletRequest request) {
		this.cropService = cropService;
		this.request = request;
	}

	@CrossOrigin
	@Override
	public ResponseEntity<CommonCropNamesResponse> commoncropnamesGet(
			@Valid @RequestParam(value = "page", required = false) Integer page,
			@Valid @RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestHeader(value = "Authorization", required = false) String authorization) throws BrAPIServerException {

		validateAcceptHeader(request);
		validateSecurityContext(request, "ROLE_ANONYMOUS", "ROLE_USER");
		log.debug("Request: " + request.getRequestURI());

		Metadata metadata = generateMetaDataTemplate(page, pageSize);
		List<String> crops = cropService.getCrops(metadata);
		return responseOK(new CommonCropNamesResponse(), new CommonCropNamesResponseResult(), crops, metadata);
	}

	@CrossOrigin
	@Override
	public ResponseEntity<CommonCropListResponse> commoncropnamesCropsPost(
			@Valid @RequestBody CommonCropNewRequest body,
			@RequestHeader(value = "Authorization", required = false) String authorization) throws BrAPIServerException {

		validateAcceptHeader(request);
		validateSecurityContext(request, "ROLE_USER");
		log.debug("Request: " + request.getRequestURI());

		CommonCrop crop = cropService.createCrop(body.getCommonCropName());
		return responseOK(new CommonCropListResponse(), new CommonCropListResponseResult(), List.of(crop));
	}

	@CrossOrigin
	@Override
	public ResponseEntity<CommonCropListResponse> commoncropnamesCropsCropDbIdPut(
			@PathVariable("cropDbId") String cropDbId,
			@Valid @RequestBody CommonCropNewRequest body,
			@RequestHeader(value = "Authorization", required = false) String authorization) throws BrAPIServerException {

		validateAcceptHeader(request);
		validateSecurityContext(request, "ROLE_USER");
		log.debug("Request: " + request.getRequestURI());

		CommonCrop crop = cropService.updateCrop(cropDbId, body.getCommonCropName());
		return responseOK(new CommonCropListResponse(), new CommonCropListResponseResult(), List.of(crop));
	}

	@CrossOrigin
	@Override
	public ResponseEntity<CommonCropListResponse> commoncropnamesCropsCropDbIdDelete(
			@PathVariable("cropDbId") String cropDbId,
			@RequestHeader(value = "Authorization", required = false) String authorization) throws BrAPIServerException {

		validateAcceptHeader(request);
		validateSecurityContext(request, "ROLE_USER");
		log.debug("Request: " + request.getRequestURI());

		CommonCrop crop = cropService.deleteCrop(cropDbId);
		return responseOK(new CommonCropListResponse(), new CommonCropListResponseResult(), List.of(crop));
	}

}

