package com.seer.teach.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.seer.teach.admin.request.UnitsQueryReq;
import com.seer.teach.admin.request.UnitsReq;
import com.seer.teach.admin.request.CheckAndScoreReq;
import com.seer.teach.admin.request.BatchCheckReq;
import com.seer.teach.admin.request.UnitMappingQueryReq;
import com.seer.teach.admin.request.UpdateExampleQuestionsHtmlReq;
import com.seer.teach.admin.response.AdminUnitsResp;
import com.seer.teach.admin.response.UnitGradeSubjectResp;
import com.seer.teach.admin.response.UnitMaterialInfoResp;
import com.seer.teach.admin.service.AdminUnitsService;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
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-23
 */
@RestController
@RequestMapping("/teacher/units")
@RequiredArgsConstructor
@Tag(name = "管理端 - 单元管理")
@Validated
public class AdminUnitsController {

    private final AdminUnitsService adminUnitsService;

    @PostMapping("/page-list")
    @Operation(summary = "分页查询单元列表")
    @SaCheckPermission("teacher:admin:units:list")
    public ResultBean<PageListBean<AdminUnitsResp>> getPageList(@RequestBody @Validated UnitsQueryReq req) {
        PageListBean<AdminUnitsResp> result = adminUnitsService.getPageList(req);
        return ResultBean.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取单元信息详情")
    @SaCheckPermission("teacher:admin:units:update")
    public ResultBean<AdminUnitsResp> getUnitDetail(@PathVariable("id") Integer id) {
        return ResultBean.success(adminUnitsService.getUnitDetailById(id));
    }

    @PostMapping("/create")
    @Operation(summary = "创建单元信息")
    @SaCheckPermission("teacher:admin:units:create")
    public ResultBean<Boolean> createdUnits(@RequestBody @Valid UnitsReq req) {
        boolean result = adminUnitsService.createdUnits(req);
        return ResultBean.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除单元信息")
    @SaCheckPermission("teacher:admin:units:delete")
    public ResultBean<Boolean> deleteUnits(@PathVariable("id") Integer id) {
        boolean result = adminUnitsService.deleteUnits(id);
        return ResultBean.success(result);
    }

    @PutMapping("/{id}")
    @Operation(summary = "修改单元信息")
    @SaCheckPermission("teacher:admin:units:update")
    public ResultBean<Boolean> updateUnits(@PathVariable("id") Integer id, @RequestBody @Valid UnitsReq req) {
        Boolean result = adminUnitsService.updateUnits(id, req);
        return ResultBean.success(result);
    }

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

    @PutMapping("/batch-check")
    @Operation(summary = "批量更新单元检查状态")
    @SaCheckPermission("teacher:admin:units:update")
    public ResultBean<Boolean> batchUpdateUnitsCheck(@Parameter(description = "批量检查状态信息") @Valid @RequestBody BatchCheckReq req) {
        boolean result = adminUnitsService.batchUpdateUnitsCheck(req.getIds(), req.getIsChecked());
        return ResultBean.success(result);
    }
    
    @PutMapping("/{id}/update-example-questions-html")
    @Operation(summary = "更新单元下所有知识点的示例题目的富文本")
    @SaCheckPermission("teacher:admin:units:update")
    public ResultBean<Boolean> updateExampleQuestionsHtml(@Parameter(description = "单元ID") @PathVariable @NotNull(message = "单元ID不能为空") Integer id,
                                                         @Parameter(description = "更新参数") @Valid @RequestBody UpdateExampleQuestionsHtmlReq req) {
        boolean result = adminUnitsService.updateExampleQuestionsHtml(id, req.getTemplateId());
        return ResultBean.success(result);
    }
    
    @GetMapping("/{id}/grade-subject")
    @Operation(summary = "根据单元ID获取年级和学科信息")
    @SaCheckPermission("teacher:admin:units:list")
    public ResultBean<UnitGradeSubjectResp> getUnitGradeAndSubject(@Parameter(description = "单元ID") @PathVariable @NotNull(message = "单元ID不能为空") Integer id) {
        UnitGradeSubjectResp result = adminUnitsService.getUnitGradeAndSubject(id);
        return ResultBean.success(result);
    }
    
    @PostMapping("/mapping-info")
    @Operation(summary = "根据知识点ID获取对应的单元信息和教材版本信息")
    @SaCheckPermission("teacher:admin:units:list")
    public ResultBean<List<UnitMaterialInfoResp>> getUnitMaterialInfo(@Parameter(description = "查询参数") @Valid @RequestBody UnitMappingQueryReq req) {
        List<UnitMaterialInfoResp> result = adminUnitsService.getUnitMaterialInfo(req);
        return ResultBean.success(result);
    }

    @GetMapping("/{id}/material-info")
    @Operation(summary = "根据知识点ID获取对应的单元信息和教材版本信息")
    @SaCheckPermission("teacher:admin:units:list")
    public ResultBean<List<UnitMaterialInfoResp>> getMaterialInfo(@Parameter(description = "知识点ID") @PathVariable("id") @NotNull(message = "知识点ID不能为空") Integer id) {
        List<UnitMaterialInfoResp> result = adminUnitsService.getMaterialInfo(id);
        return ResultBean.success(result);
    }
}