package com.zt.plat.module.base.controller.admin.templtp.onlyoffice.controller;


import com.zt.plat.framework.tenant.core.aop.TenantIgnore;
import com.zt.plat.module.base.controller.admin.templtp.onlyoffice.pojo.OnlyOfficeCallback;
import com.zt.plat.module.base.controller.admin.templtp.onlyoffice.service.OnlyOfficeCallbackService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.security.PermitAll;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/base/onlyoffice")
@Tag(name = "管理后台 - onlyOffice回调")
public class OnlyOfficeCallbackController {

    private final OnlyOfficeCallbackService callbackService;

    public OnlyOfficeCallbackController(OnlyOfficeCallbackService callbackService) {
        this.callbackService = callbackService;
    }

    /**
     * 处理OnlyOffice文档编辑服务发送的回调
     */
    @PostMapping("/callback/{id}")
    @PermitAll
    @TenantIgnore
    public ResponseEntity<Map<String, Object>> handleCallback(@RequestBody OnlyOfficeCallback callback, @PathVariable String id,@RequestParam("fileName") String fileName) {
        // 处理回调逻辑
        callbackService.processCallback(callback,id,fileName);
        log.info("回调参数：【{}】",callback.toString());
        // 返回必须的响应，否则OnlyOffice会显示错误
        Map<String, Object> response = new HashMap<>();
        response.put("error", 0);
      //  response.put("version", 100);
        return new ResponseEntity<>(response, HttpStatus.OK);
    }

    /**
     * 处理OnlyOffice文档编辑服务发送的回调
     */
    @PostMapping("/contract/callback/{id}")
    @PermitAll
    @TenantIgnore
    public ResponseEntity<Map<String, Integer>> handleContractCallback(@RequestBody OnlyOfficeCallback callback, @PathVariable String id,@RequestParam("fileName") String fileName) {
        // 处理回调逻辑
        callbackService.processContractCallback(callback,id,fileName);
        // 返回必须的响应，否则OnlyOffice会显示错误
        Map<String, Integer> response = new HashMap<>();
        response.put("error", 0);
     //   response.put("version", 0);
        return new ResponseEntity<>(response, HttpStatus.OK);
    }
}
