package cn.iocoder.byzs.module.aicourse.controller.admin.aiCourseConfig;

import cn.iocoder.byzs.module.aicourse.controller.admin.aiCourseConfig.vo.AiCourseConfigPageReqVO;
import cn.iocoder.byzs.module.aicourse.controller.admin.aiCourseConfig.vo.AiCourseConfigRespVO;
import cn.iocoder.byzs.module.aicourse.controller.admin.aiCourseConfig.vo.AiCourseConfigSaveReqVO;
import cn.iocoder.byzs.module.aicourse.dal.dataobject.aiCourseConfig.AiCourseConfigDO;
import cn.iocoder.byzs.module.aicourse.service.aiCourseConfig.AiCourseConfigService;
import cn.iocoder.byzs.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.byzs.framework.common.pojo.CommonResult;
import cn.iocoder.byzs.framework.common.pojo.PageParam;
import cn.iocoder.byzs.framework.common.pojo.PageResult;
import cn.iocoder.byzs.framework.common.util.object.BeanUtils;
import cn.iocoder.byzs.framework.excel.core.util.ExcelUtils;
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.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.byzs.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.byzs.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 课程配置")
@RestController
@RequestMapping("/aiCourse/course-config")
@Validated
public class AiCourseConfigController {

    @Resource
    private AiCourseConfigService aiCourseConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建课程配置")
    @PreAuthorize("@ss.hasPermission('aiCourse:course-config:create')")
    public CommonResult<Long>createAiCourseConfig(@Valid @RequestBody AiCourseConfigSaveReqVO createReqVO) {
        return success(aiCourseConfigService.createAiCourseConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课程配置")
    @PreAuthorize("@ss.hasPermission('aiCourse:course-config:update')")
    public CommonResult<Boolean> updateAiCourseConfig(@Valid @RequestBody AiCourseConfigSaveReqVO updateReqVO) {
        aiCourseConfigService.updateAiCourseConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除课程配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('aiCourse:course-config:delete')")
    public CommonResult<Boolean> deleteAiCourseConfig(@RequestParam("id") Long id) {
        aiCourseConfigService.deleteAiCourseConfig(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除课程配置")
                @PreAuthorize("@ss.hasPermission('aiCourse:course-config:delete')")
    public CommonResult<Boolean> deleteAiCourseConfigList(@RequestParam("ids") List<Long> ids) {
        aiCourseConfigService.deleteAiCourseConfigListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得课程配置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('aiCourse:course-config:query')")
    public CommonResult<AiCourseConfigRespVO> getAiCourseConfig(@RequestParam("id") Long id) {
        AiCourseConfigDO courseConfig = aiCourseConfigService.getAiCourseConfig(id);
        return success(BeanUtils.toBean(courseConfig, AiCourseConfigRespVO.class));
    }

    @GetMapping("/getConfigQuest")
    @Operation(summary = "获得课程配置试题列表")
    @Parameter(name = "ccCourseId", description = "课程id", required = true, example = "1025")
    @PreAuthorize("@ss.hasPermission('aiCourse:course-config:query')")
    public CommonResult<List<AiCourseConfigRespVO>> getAiCourseConfigQuestion(@RequestParam("ccCourseId") Long ccCourseId) {
        List<AiCourseConfigRespVO> courseConfigList = aiCourseConfigService.getAiCourseConfigQuestion(ccCourseId);
        return success(courseConfigList);
    }

    @GetMapping("/page")
    @Operation(summary = "获得课程配置分页")
    @PreAuthorize("@ss.hasPermission('aiCourse:course-config:query')")
    public CommonResult<PageResult<AiCourseConfigPageReqVO>> getAiCourseConfigPage(@Valid AiCourseConfigPageReqVO pageReqVO) {
        PageResult<AiCourseConfigPageReqVO> pageResult = aiCourseConfigService.getAiCourseConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AiCourseConfigPageReqVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课程配置 Excel")
    @PreAuthorize("@ss.hasPermission('aiCourse:course-config:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAiCourseConfigExcel(@Valid AiCourseConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AiCourseConfigPageReqVO> list = aiCourseConfigService.getAiCourseConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "课程配置.xls", "数据", AiCourseConfigPageReqVO.class,
                        BeanUtils.toBean(list, AiCourseConfigPageReqVO.class));
    }

}