package com.flowable.local.work.rest;

import com.fasterxml.jackson.databind.JsonNode;
import com.flowable.local.work.service.VariableService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.*;

/*
http://localhost:8090/custom-api/variable/CAS-e5d25542-0947-11f0-8b0d-e6be909bfdcd/caseNname
http://localhost:8090/custom-api/variable/CAS-e5d25542-0947-11f0-8b0d-e6be909bfdcd
http://localhost:8090/custom-api/variable/root

http://localhost:8090/platform-api/case-instances/CAS-5dc9c774-42d4-11f0-b269-2e48f5a806de/work-form/variables
http://localhost:8090/custom-api/variable/CAS-5dc9c774-42d4-11f0-b269-2e48f5a806de/name
 */

@RestController
@RequestMapping("/custom-api")
public class VariableController {

    protected final VariableService variableService;

    public VariableController(VariableService variableService) {
        this.variableService = variableService;
    }

    @GetMapping("/variable/{caseInstanceId}")
    public ResponseEntity<JsonNode> getRootVariable(@PathVariable String caseInstanceId) {
        return ResponseEntity.ok(variableService.getVariable(caseInstanceId, "root"));
    }

    @GetMapping("/variable/{caseInstanceId}/{variableName}")
    public ResponseEntity<JsonNode> getSpecificVariable(@PathVariable String caseInstanceId, @PathVariable String variableName) {
        return ResponseEntity.ok(variableService.getVariable(caseInstanceId, variableName));
    }

    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public String handleMissingServletRequestParameterException() {
        return "Variable not found";
    }
}
