package com.writeoff.module.audit.controller;

import com.writeoff.common.api.ApiResponse;
import com.writeoff.common.api.PageResult;
import com.writeoff.module.audit.dto.CreateAuditFlowRequest;
import com.writeoff.module.audit.dto.UpdateAuditFlowRequest;
import com.writeoff.module.audit.model.AuditFlowInfo;
import com.writeoff.module.audit.service.AuditFlowManageService;
import com.writeoff.security.DataScopeType;
import com.writeoff.security.RequirePermission;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController
@RequestMapping("/api/audit-flows")
public class AuditFlowController {
    private final AuditFlowManageService auditFlowManageService;

    public AuditFlowController(AuditFlowManageService auditFlowManageService) {
        this.auditFlowManageService = auditFlowManageService;
    }

    @GetMapping
    @RequirePermission(value = "audit.flow.read", dataScope = DataScopeType.TENANT, auditAction = "AUDIT_FLOW_LIST")
    public ApiResponse<PageResult<AuditFlowInfo>> list(
            @RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize) {
        return ApiResponse.success(auditFlowManageService.list(pageNo, pageSize));
    }

    @PostMapping
    @RequirePermission(value = "audit.flow.manage", dataScope = DataScopeType.TENANT, auditAction = "AUDIT_FLOW_CREATE")
    public ApiResponse<AuditFlowInfo> create(@RequestBody @Valid CreateAuditFlowRequest request) {
        return ApiResponse.success(auditFlowManageService.create(request));
    }

    @PutMapping("/{id}")
    @RequirePermission(value = "audit.flow.manage", dataScope = DataScopeType.TENANT, auditAction = "AUDIT_FLOW_UPDATE")
    public ApiResponse<AuditFlowInfo> update(@PathVariable("id") Long id, @RequestBody @Valid UpdateAuditFlowRequest request) {
        return ApiResponse.success(auditFlowManageService.update(id, request));
    }

    @PostMapping("/{id}/copy")
    @RequirePermission(value = "audit.flow.manage", dataScope = DataScopeType.TENANT, auditAction = "AUDIT_FLOW_COPY")
    public ApiResponse<AuditFlowInfo> copy(@PathVariable("id") Long id) {
        return ApiResponse.success(auditFlowManageService.copy(id));
    }

    @PostMapping("/{id}/default")
    @RequirePermission(value = "audit.flow.manage", dataScope = DataScopeType.TENANT, auditAction = "AUDIT_FLOW_SET_DEFAULT")
    public ApiResponse<String> setDefault(@PathVariable("id") Long id) {
        auditFlowManageService.setDefault(id);
        return ApiResponse.success("OK");
    }

    @PostMapping("/{id}/enable")
    @RequirePermission(value = "audit.flow.manage", dataScope = DataScopeType.TENANT, auditAction = "AUDIT_FLOW_ENABLE")
    public ApiResponse<String> enable(@PathVariable("id") Long id) {
        auditFlowManageService.enable(id);
        return ApiResponse.success("OK");
    }

    @PostMapping("/{id}/disable")
    @RequirePermission(value = "audit.flow.manage", dataScope = DataScopeType.TENANT, auditAction = "AUDIT_FLOW_DISABLE")
    public ApiResponse<String> disable(@PathVariable("id") Long id) {
        auditFlowManageService.disable(id);
        return ApiResponse.success("OK");
    }

    @PostMapping("/{id}/delete")
    @RequirePermission(value = "audit.flow.manage", dataScope = DataScopeType.TENANT, auditAction = "AUDIT_FLOW_DELETE")
    public ApiResponse<String> delete(@PathVariable("id") Long id) {
        auditFlowManageService.softDelete(id);
        return ApiResponse.success("OK");
    }
}
