package com.flowable.local.work.rest;

import com.flowable.local.work.service.AuditTrailService;
import org.flowable.common.rest.api.DataResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.HashMap;

/*
Examples:
http://localhost:8090/custom-api/audit-trail?scopeType=dataObject&dataObjectModelKey=TST_DO001
http://localhost:8090/custom-api/audit-trail?scopeType=dataObject&dataObjectId=d05bd02b-3f8a-11f0-9e89-2e48f5a806de
http://localhost:8090/custom-api/audit-trail?scopeType=dataObject&scopeId=d05bd02b-3f8a-11f0-9e89-2e48f5a806de
http://localhost:8090/custom-api/audit-trail?scopeType=cmmn&instanceName=TST-000001
http://localhost:8090/custom-api/audit-trail?scopeType=cmmn&rootId=CAS-9270a424-3fa3-11f0-8c07-2e48f5a806de
http://localhost:8090/custom-api/audit-trail?scopeType=cmmn&scopeId=CAS-9270a424-3fa3-11f0-8c07-2e48f5a806de
*/

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

    protected final AuditTrailService auditTrailService;

    public AuditTrailController(AuditTrailService auditTrailService) {
        this.auditTrailService = auditTrailService;
    }

    @GetMapping(value = {"/audit-trail"}, produces = {"application/json"})
    public DataResponse<HashMap<String, Object>> getAuditTrail(
            @RequestParam(name = "scopeType") @NotNull @NotEmpty String scopeType,
            @RequestParam(name = "scopeId", required = false) String scopeId,
            @RequestParam(name = "rootId", required = false) String rootId,
            @RequestParam(name = "instanceName", required = false) String instanceName,
            @RequestParam(name = "dataObjectId", required = false) String dataObjectId,
            @RequestParam(name = "dataObjectModelKey", required = false) String dataObjectModelKey,
            @RequestParam(name = "start", required = false) Integer start,
            @RequestParam(name = "size", required = false) Integer size,
            @RequestParam(name = "createdAfter", required = false) String createdAfter,
            @RequestParam(name = "createdBefore", required = false) String createdBefore,
            @RequestParam(name = "creatorId", required = false) String creatorId,
            @RequestParam(name = "subType", required = false) String subType,
            @RequestParam(name = "subScopeId", required = false) String subScopeId) {

        return this.auditTrailService.getAuditTrail(scopeType, scopeId, rootId, instanceName, dataObjectId, dataObjectModelKey,
                start, size, createdAfter, createdBefore, creatorId, subType, subScopeId);
    }
}
