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.BatchCheckReq;
import com.seer.teach.admin.request.CheckAndScoreReq;
import com.seer.teach.admin.request.ExampleQuestionsQueryReq;
import com.seer.teach.admin.request.ExampleQuestionsReq;
import com.seer.teach.admin.request.GenerateSimilarQuestionReq;
import com.seer.teach.admin.response.ExampleQuestionsResp;
import com.seer.teach.admin.service.AdminExampleQuestionsService;
import com.seer.teach.admin.service.ExampleQuestionGenerationService;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.enums.AiScenarioCodeEnum;
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.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.RestController;

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

/**
 * <p>
 * 例题管理控制器
 * </p>
 *
 * @author ZS
 * @since 2025-10-24
 */
@RestController
@RequestMapping("/teacher/example-questions")
@RequiredArgsConstructor
@Tag(name = "管理端 - 例题管理")
@Validated
public class AdminExampleQuestionsController {

    private final AdminExampleQuestionsService adminExampleQuestionsService;
    
    private final ExampleQuestionGenerationService exampleQuestionGenerationService;

    @PostMapping("/page-list")
    @Operation(summary = "分页查询例题列表")
    @SaCheckPermission("teacher:admin:example-questions:list")
    public ResultBean<PageListBean<ExampleQuestionsResp>> pageExampleQuestions(@RequestBody @Validated ExampleQuestionsQueryReq req) {
        IPage<ExampleQuestionsResp> page = adminExampleQuestionsService.pageExampleQuestions(req);
        return ResultBean.success(new PageListBean<>(page));
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取例题详情")
    @SaCheckPermission("teacher:admin:example-questions:detail")
    public ResultBean<ExampleQuestionsResp> getExampleQuestionById(@Parameter(description = "例题ID") @PathVariable @NotNull(message = "例题ID不能为空") Integer id) {
        ExampleQuestionsResp exampleQuestion = adminExampleQuestionsService.getExampleQuestionById(id);
        return ResultBean.success(exampleQuestion);
    }

    @PostMapping
    @Operation(summary = "添加例题")
    @SaCheckPermission("teacher:admin:example-questions:save")
    public ResultBean<Boolean> addExampleQuestion(@Parameter(description = "例题信息") @Valid @RequestBody ExampleQuestionsReq req) {
        boolean result = adminExampleQuestionsService.addExampleQuestion(req);
        return ResultBean.success(result);
    }

    @PostMapping("/batch-save-generated")
    @Operation(summary = "批量保存生成的例题")
    @SaCheckPermission("teacher:admin:example-questions:batch-save")
    public ResultBean<Boolean> batchSaveGeneratedExampleQuestions(@Parameter(description = "例题信息列表") @Valid @RequestBody List<ExampleQuestionsReq> reqList) {
        boolean result = adminExampleQuestionsService.batchSaveGeneratedExampleQuestions(reqList);
        return ResultBean.success(result);
    }

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

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

    @PutMapping("/batch-check")
    @Operation(summary = "批量更新例题检查状态")
    @SaCheckPermission("teacher:admin:example-questions:batch-update-check")
    public ResultBean<Boolean> batchUpdateExampleQuestionCheck(@Parameter(description = "批量检查状态信息") @Valid @RequestBody BatchCheckReq req) {
        boolean result = adminExampleQuestionsService.batchUpdateExampleQuestionCheck(req.getIds(), req.getIsChecked());
        return ResultBean.success(result);
    }

    @PostMapping("/generate-similar")
    @Operation(summary = "根据示例题目生成类似题目")
    @SaCheckPermission("teacher:admin:example-questions:generate")
    public ResultBean<List<ExampleQuestionsResp>> generateSimilarQuestions(@Parameter(description = "生成题目参数") @Valid @RequestBody GenerateSimilarQuestionReq req) {
        req.setScenarioCode(AiScenarioCodeEnum.KNOWLEDGE_POINT_GENERATE_EXAMPLE_QUESTIONS.getCode());
        req.setUserId(StpUtil.getLoginIdAsInt());
        List<ExampleQuestionsResp> result = exampleQuestionGenerationService.generateSimilarQuestions(req);
        return ResultBean.success(result);
    }

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