package com.xunhong.erp.turbo.facade.app.executor.cmd;

import com.alibaba.fastjson2.JSONObject;
import com.xunhong.erp.turbo.api.facade.dto.cmd.VehicleExtractionCmd;
import com.xunhong.erp.turbo.api.facade.dto.vo.VehicleExtractionVO;
import com.xunhong.erp.turbo.api.infra.api.SettingServiceI;
import com.xunhong.erp.turbo.api.infra.dto.common.SmartRecognitionPromptValue;
import com.xunhong.erp.turbo.api.infra.dto.enums.SettingKeyEnum;
import com.xunhong.erp.turbo.api.infra.dto.qry.SettingShowQry;
import com.xunhong.erp.turbo.api.infra.dto.vo.SettingVO;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class VehicleExtractionCmdExe {

	private final DeepSeekChatModel deepSeekChatModel;

	@DubboReference(version = "1.0.0")
	private final SettingServiceI settingService;

	@SneakyThrows
	public VehicleExtractionVO execute(VehicleExtractionCmd vehicleExtractionCmd) {
		SettingShowQry settingShowQry = new SettingShowQry();
		settingShowQry.setSettingKey(SettingKeyEnum.SMART_RECOGNITION_PROMPT);
		SettingVO settingVO = settingService.show(settingShowQry);
		SmartRecognitionPromptValue smartRecognitionPromptValue = (SmartRecognitionPromptValue) settingVO.getSettingValue();

		SystemMessage systemMessage = SystemMessage.builder().text(smartRecognitionPromptValue.getPrompt().replace("{经销商列表}", vehicleExtractionCmd.getDealerNames())).build();

		UserMessage userMessage = UserMessage.builder().text(vehicleExtractionCmd.getMessage()).build();
		Prompt prompt = Prompt.builder().messages(List.of(systemMessage, userMessage)).build();
		ChatResponse chatResponse = deepSeekChatModel.call(prompt);

		return JSONObject.parseObject(chatResponse.getResult().getOutput().getText(), VehicleExtractionVO.class);
	}
}
