package org.dromara.ai.controller.app;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.ai.domain.bo.AiRatingRecordBo;
import org.dromara.ai.domain.vo.AiRatingRecordVo;
import org.dromara.ai.service.AiRatingRecordService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * AI评价记录（用户端）
 *
 * @author pengles
 */
@Tag(name = "用户端-AI评价记录", description = "AI评价记录接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/ai/rating")
public class AppAiRatingRecordController extends BaseController {

    private final AiRatingRecordService aiRatingRecordService;

    /**
     * 提交评价
     */
    @Operation(summary = "提交评价", description = "用户对AI回复进行评价")
    @Log(title = "AI评价", businessType = BusinessType.OTHER)
    @PostMapping("/submit")
    public R<Void> submitRating(@Valid @RequestBody AiRatingRecordBo bo) {
        // 设置当前用户ID
        Long userId = LoginHelper.getUserId();
        bo.setUserId(userId);
        return toAjax(aiRatingRecordService.insertByBo(bo));
    }

    /**
     * 查询评价记录（根据消息ID）
     */
    @Operation(summary = "查询评价记录", description = "根据消息ID查询当前用户的评价记录")
    @GetMapping("/{messageId}")
    public R<AiRatingRecordVo> getRating(@Parameter(description = "消息ID", required = true)
                                          @NotNull(message = "消息ID不能为空")
                                          @PathVariable String messageId) {
        Long userId = LoginHelper.getUserId();
        AiRatingRecordVo vo = aiRatingRecordService.queryByMessageId(messageId, userId);
        return R.ok(vo);
    }

}
