package com.seer.teach.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.seer.teach.admin.request.MaterialQueryReq;
import com.seer.teach.admin.request.MaterialReq;
import com.seer.teach.admin.request.CheckAndScoreReq;
import com.seer.teach.admin.request.UpdateExampleQuestionsHtmlReq;
import com.seer.teach.admin.response.MaterialResp;
import com.seer.teach.admin.response.MaterialWithUnitResp;
import com.seer.teach.admin.service.AdminsMaterialService;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.annotation.LogPrint;
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.*;

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

/**
 * <p>
 * 教材管理控制器
 * </p>
 *
 * @author ZS
 * @since 2025-10-22
 */
@RestController
@RequestMapping("/teacher/material")
@RequiredArgsConstructor
@LogPrint
@Tag(name = "管理端 - 教材管理")
public class AdminMaterialController {

    private final AdminsMaterialService adminsMaterialService;

    @PostMapping("/page-list")
    @Operation(summary = "分页查询教材列表")
    @SaCheckPermission("teacher:admin:material:list")
    public ResultBean<PageListBean<MaterialResp>> pageMaterials(@RequestBody @Validated MaterialQueryReq req) {
        IPage<MaterialResp> page = adminsMaterialService.pageMaterials(req);
        return ResultBean.success(new PageListBean<>(page));
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取教材详情")
    @SaCheckPermission("teacher:admin:material:detail")
    public ResultBean<MaterialResp> getMaterialById(@Parameter(description = "教材ID") @PathVariable @NotNull(message = "教材ID不能为空") Integer id) {
        MaterialResp material = adminsMaterialService.getMaterialById(id);
        return ResultBean.success(material);
    }

    @GetMapping("/by-knowledge-point/{knowledgePointId}")
    @Operation(summary = "根据知识点ID获取教材列表")
    @SaCheckPermission("teacher:admin:material:list")
    public ResultBean<List<MaterialWithUnitResp>> getMaterialsByKnowledgePointId(@Parameter(description = "知识点ID") @PathVariable @NotNull(message = "知识点ID不能为空") Integer knowledgePointId) {
        List<MaterialWithUnitResp> materials = adminsMaterialService.getMaterialsByKnowledgePointId(knowledgePointId);
        return ResultBean.success(materials);
    }

    @PostMapping
    @Operation(summary = "添加教材")
    @SaCheckPermission("teacher:admin:material:save")
    public ResultBean<Boolean> addMaterial(@Parameter(description = "教材信息") @Valid @RequestBody MaterialReq req) {
        boolean result = adminsMaterialService.addMaterial(req);
        return ResultBean.success(result);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新教材信息")
    @SaCheckPermission("teacher:admin:material:update")
    public ResultBean<Boolean> updateMaterial(@Parameter(description = "教材ID") @PathVariable @NotNull(message = "教材ID不能为空") Integer id,
                                              @Parameter(description = "教材信息") @Valid @RequestBody MaterialReq req) {
        boolean result = adminsMaterialService.updateMaterial(id, req);
        return ResultBean.success(result);
    }

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

    @DeleteMapping("/{id}")
    @Operation(summary = "删除教材")
    @SaCheckPermission("teacher:admin:material:delete")
    public ResultBean<Boolean> deleteMaterial(@Parameter(description = "教材ID") @PathVariable @NotNull(message = "教材ID不能为空") Integer id) {
        boolean result = adminsMaterialService.deleteMaterial(id);
        return ResultBean.success(result);
    }
    
    @PutMapping("/{id}/update-example-questions-html")
    @Operation(summary = "更新教材下所有单元、知识点的示例题目的富文本")
    @SaCheckPermission("teacher:admin:material:update")
    public ResultBean<Boolean> updateExampleQuestionsHtml(@Parameter(description = "教材ID") @PathVariable @NotNull(message = "教材ID不能为空") Integer id,
                                                         @Parameter(description = "更新参数") @Valid @RequestBody UpdateExampleQuestionsHtmlReq req) {
        boolean result = adminsMaterialService.updateExampleQuestionsHtml(id, req.getTemplateId());
        return ResultBean.success(result);
    }
}