package com.yolo.keyboard.controller.admin.usercalllog;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yolo.keyboard.framework.common.pojo.PageParam;
import com.yolo.keyboard.framework.common.pojo.PageResult;
import com.yolo.keyboard.framework.common.pojo.CommonResult;
import com.yolo.keyboard.framework.common.util.object.BeanUtils;
import static com.yolo.keyboard.framework.common.pojo.CommonResult.success;

import com.yolo.keyboard.framework.excel.core.util.ExcelUtils;

import com.yolo.keyboard.framework.apilog.core.annotation.ApiAccessLog;
import static com.yolo.keyboard.framework.apilog.core.enums.OperateTypeEnum.*;

import com.yolo.keyboard.controller.admin.usercalllog.vo.*;
import com.yolo.keyboard.dal.dataobject.usercalllog.KeyboardUserCallLogDO;
import com.yolo.keyboard.service.usercalllog.KeyboardUserCallLogService;

@Tag(name = "管理后台 - 用户每次调用日志（用于记录token、模型、耗时、成功率等）")
@RestController
@RequestMapping("/keyboard/user-call-log")
@Validated
public class KeyboardUserCallLogController {

    @Resource
    private KeyboardUserCallLogService userCallLogService;

    @PostMapping("/create")
    @Operation(summary = "创建用户每次调用日志（用于记录token、模型、耗时、成功率等）")
    @PreAuthorize("@ss.hasPermission('keyboard:user-call-log:create')")
    public CommonResult<Long> createUserCallLog(@Valid @RequestBody KeyboardUserCallLogSaveReqVO createReqVO) {
        return success(userCallLogService.createUserCallLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户每次调用日志（用于记录token、模型、耗时、成功率等）")
    @PreAuthorize("@ss.hasPermission('keyboard:user-call-log:update')")
    public CommonResult<Boolean> updateUserCallLog(@Valid @RequestBody KeyboardUserCallLogSaveReqVO updateReqVO) {
        userCallLogService.updateUserCallLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户每次调用日志（用于记录token、模型、耗时、成功率等）")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('keyboard:user-call-log:delete')")
    public CommonResult<Boolean> deleteUserCallLog(@RequestParam("id") Long id) {
        userCallLogService.deleteUserCallLog(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除用户每次调用日志（用于记录token、模型、耗时、成功率等）")
                @PreAuthorize("@ss.hasPermission('keyboard:user-call-log:delete')")
    public CommonResult<Boolean> deleteUserCallLogList(@RequestParam("ids") List<Long> ids) {
        userCallLogService.deleteUserCallLogListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户每次调用日志（用于记录token、模型、耗时、成功率等）")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('keyboard:user-call-log:query')")
    public CommonResult<KeyboardUserCallLogRespVO> getUserCallLog(@RequestParam("id") Long id) {
        KeyboardUserCallLogDO userCallLog = userCallLogService.getUserCallLog(id);
        return success(BeanUtils.toBean(userCallLog, KeyboardUserCallLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户每次调用日志（用于记录token、模型、耗时、成功率等）分页")
    @PreAuthorize("@ss.hasPermission('keyboard:user-call-log:query')")
    public CommonResult<PageResult<KeyboardUserCallLogRespVO>> getUserCallLogPage(@Valid KeyboardUserCallLogPageReqVO pageReqVO) {
        PageResult<KeyboardUserCallLogDO> pageResult = userCallLogService.getUserCallLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KeyboardUserCallLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户每次调用日志（用于记录token、模型、耗时、成功率等） Excel")
    @PreAuthorize("@ss.hasPermission('keyboard:user-call-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserCallLogExcel(@Valid KeyboardUserCallLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<KeyboardUserCallLogDO> list = userCallLogService.getUserCallLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户每次调用日志（用于记录token、模型、耗时、成功率等）.xls", "数据", KeyboardUserCallLogRespVO.class,
                        BeanUtils.toBean(list, KeyboardUserCallLogRespVO.class));
    }

}