package org.dromara.ai.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
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.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * AI评价记录管理
 *
 * @author pengles
 */
@Tag(name = "管理端-AI评价记录管理", description = "AI评价记录管理接口")
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ai/rating/record")
public class AdminAiRatingRecordController extends BaseController {

    private final AiRatingRecordService aiRatingRecordService;

    /**
     * 查询AI评价记录列表
     */
    @Operation(summary = "查询AI评价记录列表", description = "分页查询AI评价记录列表，支持多条件搜索")
    @SaCheckPermission("ai:rating:record:list")
    @GetMapping("/list")
    public TableDataInfo<AiRatingRecordVo> list(@Parameter(description = "评价记录查询条件") AiRatingRecordBo bo,
                                                 @Parameter(description = "分页参数") PageQuery pageQuery) {
        return aiRatingRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出AI评价记录列表
     */
    @Operation(summary = "导出AI评价记录列表", description = "导出AI评价记录到Excel")
    @SaCheckPermission("ai:rating:record:export")
    @Log(title = "AI评价记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Parameter(description = "评价记录查询条件") AiRatingRecordBo bo,
                       @Parameter(description = "响应对象") HttpServletResponse response) {
        List<AiRatingRecordVo> list = aiRatingRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "AI评价记录", AiRatingRecordVo.class, response);
    }

    /**
     * 获取AI评价记录详细信息
     */
    @Operation(summary = "获取AI评价记录详情", description = "根据记录ID获取详细信息")
    @SaCheckPermission("ai:rating:record:query")
    @GetMapping("/{id}")
    public R<AiRatingRecordVo> getInfo(@Parameter(description = "记录ID", required = true)
                                        @NotNull(message = "主键不能为空")
                                        @PathVariable Long id) {
        return R.ok(aiRatingRecordService.queryById(id));
    }

    /**
     * 批量删除AI评价记录
     */
    @Operation(summary = "批量删除AI评价记录", description = "根据ID数组批量删除AI评价记录")
    @SaCheckPermission("ai:rating:record:remove")
    @Log(title = "AI评价记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@Parameter(description = "记录ID数组", required = true)
                           @NotNull(message = "主键不能为空")
                           @PathVariable Long[] ids) {
        return toAjax(aiRatingRecordService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 获取平均评分
     */
    @Operation(summary = "获取平均评分", description = "获取指定Provider的平均评分")
    @SaCheckPermission("ai:rating:record:query")
    @GetMapping("/average/{providerName}")
    public R<Map<String, Object>> getAverageRating(@Parameter(description = "Provider名称", required = true)
                                                     @PathVariable String providerName) {
        Double averageRating = aiRatingRecordService.getAverageRating(providerName);
        return R.ok(Map.of("providerName", providerName, "averageRating", averageRating));
    }

}
