package com.huimv.employment.controller.mcp;

import com.huimv.employment.common.web.R;
import com.huimv.employment.service.enterprise.McpEnterpriseProfileService;
import com.huimv.employment.service.enterprise.dto.McpEnterpriseProfileResponse;
import com.huimv.employment.service.enterprise.dto.McpEnterpriseProfileUpdateRequest;
import com.huimv.employment.service.enterprise.dto.McpEnterpriseProfileUpdateResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
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;

/**
 * MCP 企业画像接口（交互规则 v2 §6.2）。
 * <p>
 * 通过请求体/查询参数 {@code session_id} 自动关联会话与企业，Agent 无需传递 enterprise_id。
 * 鉴权见 {@code fe.internal.ai-key} 与 {@link com.huimv.employment.security.McpAuthFilter}。
 * </p>
 */
@RestController
@RequestMapping("/api/mcp/enterprise")
@Tag(name = "MCP-企业画像", description = "智能体加载/更新企业画像，需 X-Fe-Ai-Key + session_id")
public class McpEnterpriseProfileController {

    private final McpEnterpriseProfileService mcpEnterpriseProfileService;

    public McpEnterpriseProfileController(McpEnterpriseProfileService mcpEnterpriseProfileService) {
        this.mcpEnterpriseProfileService = mcpEnterpriseProfileService;
    }

    @GetMapping("/profile")
    @Operation(summary = "获取企业画像",
            description = "会话初始化时加载企业基础信息与偏好设置，供 Agent 注入上下文。"
                    + "企业尚未认证时仍返回成功，字段为空。",
            parameters = {
                    @Parameter(name = "session_id", in = ParameterIn.QUERY, required = true,
                            description = "会话标识，与 AI chat session_id / conversation_no 一致"),
                    @Parameter(name = "X-Fe-Ai-Key", in = ParameterIn.HEADER,
                            description = "内网服务密钥")
            })
    public R<McpEnterpriseProfileResponse> getProfile(@RequestParam("session_id") String sessionId) {
        return R.ok(mcpEnterpriseProfileService.getProfile(sessionId));
    }

    @PutMapping("/profile")
    @Operation(summary = "增量更新企业画像",
            description = "捕获对话中的新信息（如地址变更、结算偏好），支持 preferences.xxx 点路径。",
            parameters = {
                    @Parameter(name = "X-Fe-Ai-Key", in = ParameterIn.HEADER)
            })
    public R<McpEnterpriseProfileUpdateResponse> updateProfile(
            @Validated @RequestBody McpEnterpriseProfileUpdateRequest request) {
        return R.ok(mcpEnterpriseProfileService.updateProfile(request.getSessionId(), request));
    }
}
