package com.seer.teach.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.seer.teach.admin.request.BankQuestionsBatchSaveReq;
import com.seer.teach.admin.request.BankQuestionsQueryReq;
import com.seer.teach.admin.request.BankQuestionsReq;
import com.seer.teach.admin.request.CheckAndScoreReq;
import com.seer.teach.admin.request.GenerateBankQuestionReq;
import com.seer.teach.admin.request.GenerateTaskReq;
import com.seer.teach.admin.response.BankQuestionsResp;
import com.seer.teach.admin.service.AdminBankQuestionsService;
import com.seer.teach.admin.service.BankQuestionGenerationService;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.annotation.LogPrint;
import com.seer.teach.common.enums.AiScenarioCodeEnum;
import com.seer.teach.common.service.CommonFileService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 管理端 - 题库管理
 * </p>
 *
 * @author Lingma
 * @since 2025-10-27
 */
@RestController
@RequestMapping("/teacher/bank-questions")
@RequiredArgsConstructor
@Tag(name = "管理端 - 题库管理")
@Validated
public class AdminBankQuestionsController {

    @Autowired
    private  AdminBankQuestionsService adminBankQuestionsService;

    @Autowired
    private BankQuestionGenerationService bankQuestionGenerationService;

    @Autowired
    private  CommonFileService commonFileService;

    @PostMapping("/page-list")
    @Operation(summary = "分页查询题库列表")
    @SaCheckPermission("teacher:admin:bank-questions:list")
    public ResultBean<PageListBean<BankQuestionsResp>> pageBankQuestions(@RequestBody @Validated BankQuestionsQueryReq req) {
        IPage<BankQuestionsResp> page = adminBankQuestionsService.pageBankQuestions(req);
        return ResultBean.success(new PageListBean<>(page));
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取题库详情")
    @SaCheckPermission("teacher:admin:bank-questions:detail")
    public ResultBean<BankQuestionsResp> getBankQuestionById(@Parameter(description = "题库ID") @PathVariable @NotNull(message = "题库ID不能为空") Integer id) {
        BankQuestionsResp bankQuestion = adminBankQuestionsService.getBankQuestionById(id);
        return ResultBean.success(bankQuestion);
    }

    @PostMapping
    @Operation(summary = "添加题库")
    @SaCheckPermission("teacher:admin:bank-questions:save")
    public ResultBean<Boolean> addBankQuestion(@Parameter(description = "题库信息") @Valid @RequestBody BankQuestionsReq req) {
        boolean result = adminBankQuestionsService.addBankQuestion(req);
        return ResultBean.success(result);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新题库信息")
    @SaCheckPermission("teacher:admin:bank-questions:update")
    public ResultBean<Boolean> updateBankQuestion(@Parameter(description = "题库ID") @PathVariable @NotNull(message = "题库ID不能为空") Integer id,
                                                  @Parameter(description = "题库信息") @Valid @RequestBody BankQuestionsReq req) {
        boolean result = adminBankQuestionsService.updateBankQuestion(id, req);
        return ResultBean.success(result);
    }

    @PutMapping("/{id}/check-and-score")
    @Operation(summary = "更新题库检查状态和评分")
    @SaCheckPermission("teacher:admin:bank-questions:update")
    public ResultBean<Boolean> updateBankQuestionCheckAndScore(@Parameter(description = "题库ID") @PathVariable @NotNull(message = "题库ID不能为空") Integer id,
                                                               @Parameter(description = "检查状态和评分信息") @Valid @RequestBody CheckAndScoreReq req) {
        boolean result = adminBankQuestionsService.updateBankQuestionCheckAndScore(id, req);
        return ResultBean.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除题库")
    @SaCheckPermission("teacher:admin:bank-questions:delete")
    public ResultBean<Boolean> deleteBankQuestion(@Parameter(description = "题库ID") @PathVariable @NotNull(message = "题库ID不能为空") Integer id) {
        boolean result = adminBankQuestionsService.deleteBankQuestion(id);
        return ResultBean.success(result);
    }

    @PostMapping("/generate-similar")
    @Operation(summary = "根据知识点生成题库题目")
    @SaCheckPermission("teacher:admin:bank-questions:generate")
    public ResultBean<List<BankQuestionsResp>> generateSimilarBackQuestions(@Parameter(description = "生成题目参数") @Valid @RequestBody GenerateTaskReq req) {
        req.setUserId(StpUtil.getLoginIdAsInt());
        req.setScenarioCode(AiScenarioCodeEnum.KNOWLEDGE_POINT_GENERATE_QUESTIONS.getCode());
        List<BankQuestionsResp> result = bankQuestionGenerationService.generateBankQuestions(req);
        return ResultBean.success(result);
    }

    @PostMapping("/generate-all-and-save")
    @Operation(summary = "生成所有题库题目并保存")
    @SaCheckPermission("teacher:admin:bank-questions:generate")
    public ResultBean<Integer> generateAllBankQuestionsAndSave(@Parameter(description = "生成题目参数") @Valid @RequestBody GenerateTaskReq req) {
        req.setUserId(StpUtil.getLoginIdAsInt());
        bankQuestionGenerationService.generateAndSaveAllBankQuestions(req);
        return ResultBean.success();
    }

    @PostMapping("/batch-save-generated")
    @Operation(summary = "批量保存生成的题目")
    @SaCheckPermission("teacher:admin:bank-questions:batch-save")
    public ResultBean<Boolean> batchSaveGeneratedBankQuestions(@Parameter(description = "题目信息列表") @Valid @RequestBody BankQuestionsBatchSaveReq req) {
        boolean result = adminBankQuestionsService.batchSaveGeneratedBankQuestions(req);
        return ResultBean.success(result);
    }

    @PostMapping("/upload")
    @LogPrint
    @Operation(summary = "上传题库图片")
    public ResultBean<String> upload(@RequestParam("file") @NotNull(message = "文件不可为空") MultipartFile file)  {
        // 检查文件是否为图片类型
        String result = commonFileService.uploadQuestionImg(file);
        return ResultBean.success(result);
    }
}