package cn.iocoder.byzs.module.web.controller.admin.ai;

import cn.hutool.core.util.ObjUtil;
import cn.iocoder.byzs.framework.common.pojo.CommonResult;
import cn.iocoder.byzs.framework.common.pojo.PageResult;
import cn.iocoder.byzs.framework.common.util.object.BeanUtils;
import cn.iocoder.byzs.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.byzs.module.ai.controller.admin.chat.vo.conversation.AiChatConversationCreateMyReqVO;
import cn.iocoder.byzs.module.ai.controller.admin.chat.vo.message.AiChatMessageSendReqVO;
import cn.iocoder.byzs.module.ai.controller.admin.chat.vo.message.AiChatMessageSendRespVO;
import cn.iocoder.byzs.module.ai.controller.admin.image.vo.AiImageDrawReqVO;
import cn.iocoder.byzs.module.ai.controller.admin.image.vo.AiImageRespVO;
import cn.iocoder.byzs.module.ai.controller.admin.model.vo.chatRole.AiChatRolePageReqVO;
import cn.iocoder.byzs.module.ai.controller.admin.model.vo.chatRole.AiChatRoleRespVO;
import cn.iocoder.byzs.module.ai.dal.dataobject.image.AiImageDO;
import cn.iocoder.byzs.module.ai.dal.dataobject.model.AiChatRoleDO;
import cn.iocoder.byzs.module.ai.service.chat.AiChatConversationService;
import cn.iocoder.byzs.module.ai.service.chat.AiChatMessageService;
import cn.iocoder.byzs.module.ai.service.image.AiImageService;
import cn.iocoder.byzs.module.ai.service.model.AiChatRoleService;
import cn.iocoder.byzs.module.web.controller.admin.ai.vo.WebAiChatRoleVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.List;

import static cn.iocoder.byzs.framework.common.pojo.CommonResult.success;
import static cn.iocoder.byzs.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "Web-前端接口")
@RestController
@RequestMapping("/bjdxWeb/ai")
@Validated
public class WebAiController {

    @Resource
    private AiChatConversationService chatConversationService;
    @Resource
    private AiChatMessageService chatMessageService;
    @Resource
    private AiImageService imageService;
    @Resource
    private AiChatRoleService chatRoleService;
    
    private Long userId = 1L;


    // ================ 智能问答 ================

    @Operation(summary = "创建智能问答")
    @PostMapping("/create-dialogue")
    public CommonResult<Long> createChatConversationMy(@RequestBody @Valid AiChatConversationCreateMyReqVO createReqVO) {
        return success(chatConversationService.createChatConversationMy(createReqVO, userId));
    }

//    @PermitAll
//    @TenantIgnore
//    @Operation(summary = "智能问答-获得智能问答列表")
//    @GetMapping("/dialogue-list")
//    public CommonResult<List<AiChatConversationRespVO>> getChatConversationMyList() {
//        List<AiChatConversationDO> list = chatConversationService.getChatConversationListByUserId(getLoginUserId());
//        return success(BeanUtils.toBean(list, AiChatConversationRespVO.class));
//    }

    @Operation(summary = "智能问答-发送消息（流式）", description = "流式返回，响应较快")
    @PostMapping(value = "/dialogue-send-stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<CommonResult<AiChatMessageSendRespVO>> sendChatMessageStream(@Valid @RequestBody AiChatMessageSendReqVO sendReqVO) {
        return chatMessageService.sendChatMessageStream(sendReqVO, userId);
    }

    // ================ 绘图管理 ================

    @Operation(summary = "生成图片")
    @PostMapping("/create-painting")
    public CommonResult<Long> drawImage(@Valid @RequestBody AiImageDrawReqVO drawReqVO) {
        return success(imageService.drawImage(userId, drawReqVO));
    }

    @GetMapping("/painting-get-my")
    @Operation(summary = "绘画-获取绘图记录")
    public CommonResult<AiImageRespVO> getImageMy(@RequestParam("id") Long id) {
        AiImageDO image = imageService.getImage(id);
        if (image == null || ObjUtil.notEqual(userId, image.getUserId())) {
            return success(null);
        }
        return success(BeanUtils.toBean(image, AiImageRespVO.class));
    }

    @GetMapping("/painting-get-mys")
    @Operation(summary = "获取【我的】绘图记录列表")
    @Parameter(name = "ids", required = true, description = "绘画编号数组", example = "1024,2048")
    public CommonResult<List<AiImageRespVO>> getImageListMyByIds(@RequestParam("ids") List<Long> ids) {
        List<AiImageDO> imageList = imageService.getImageList(ids);
//        imageList.removeIf(item -> !ObjUtil.equal(getLoginUserId(), item.getUserId()));
        return success(BeanUtils.toBean(imageList, AiImageRespVO.class));
    }


    @GetMapping("/selectRoleModel")
    @Operation(summary = "获得聊天角色分页")
    public CommonResult<PageResult<WebAiChatRoleVO>> selectRoleModel(@Valid AiChatRolePageReqVO pageReqVO) {
        PageResult<AiChatRoleDO> pageResult = chatRoleService.getChatRoleMyPage(pageReqVO, userId);
        return success(BeanUtils.toBean(pageResult, WebAiChatRoleVO.class));
    }
}