package objective.taskboard.wip;

import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.http.HttpStatus.OK;

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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/wip-validator")
public class WipValidatorController {

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

    @Autowired
    private WipValidatorService wipValidatorService;

    @RequestMapping
    public ResponseEntity<WipValidatorResponse> validate(
            @RequestParam("issue") String issueKey,
            @RequestParam("status") String newStatusName) {
        WipValidatorResponse response = new WipValidatorResponse();

        try {
            response = wipValidatorService.validate(issueKey, newStatusName);
            return new ResponseEntity<WipValidatorResponse>(response, OK);
        } catch (Exception e) {
            log.error("Wip validation failed", e);
            response.message = e.getMessage() == null ? e.toString() : e.getMessage();
            return new ResponseEntity<WipValidatorResponse>(response, INTERNAL_SERVER_ERROR);
        }
    }

}
