package com.zhengmeng.ocrplatform.fill;

import com.zhengmeng.ocrplatform.integration.PluginIntegrationInterceptor;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/plugins/fill")
public class PluginFillDataController {
    private final FillDataService fillDataService;

    public PluginFillDataController(FillDataService fillDataService) {
        this.fillDataService = fillDataService;
    }

    @GetMapping("/tasks/{taskId}/form3")
    public FillDataContractResponse getForm3FillDataForPlugin(@PathVariable String taskId,
                                                             @RequestParam(required = false) String templateCode,
                                                             HttpServletRequest request) {
        FillDataPayload payload = fillDataService.buildForm3Payload(taskId, templateCode, clientCode(request));
        return new FillDataContractResponse(0, "success", payload);
    }

    private String clientCode(HttpServletRequest request) {
        Object value = request.getAttribute(PluginIntegrationInterceptor.REQUEST_CLIENT_CODE);
        return value instanceof String clientCode ? clientCode : null;
    }
}
