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.CheckAndScoreReq;
import com.seer.teach.admin.request.MaterialDocQueryReq;
import com.seer.teach.admin.request.MaterialDocReq;
import com.seer.teach.admin.request.MaterialDocSaveReq;
import com.seer.teach.admin.response.MaterialDocResp;
import com.seer.teach.admin.service.AdminMaterialDocService;
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.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.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import java.io.IOException;

/**
 * <p>
 * 教材文档管理控制器
 * </p>
 *
 * @author ZS
 * @since 2025-10-23
 */
@RestController
@RequestMapping("/teacher/material-doc")
@RequiredArgsConstructor
@Tag(name = "管理端 - 教材文档管理")
@Validated
public class AdminMaterialDocController {

    private final AdminMaterialDocService adminMaterialDocService;

    @PostMapping("/page-list")
    @Operation(summary = "分页查询教材文档列表")
    @SaCheckPermission("teacher:admin:material-doc:list")
    public ResultBean<PageListBean<MaterialDocResp>> pageMaterialDocs(@RequestBody @Validated MaterialDocQueryReq req) {
        IPage<MaterialDocResp> page = adminMaterialDocService.pageMaterialDocs(req);
        return ResultBean.success(new PageListBean<>(page));
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取教材文档详情")
    @SaCheckPermission("teacher:admin:material-doc:detail")
    public ResultBean<MaterialDocResp> getMaterialDocById(@Parameter(description = "教材文档ID") @PathVariable @NotNull(message = "教材文档ID不能为空") Long id) {
        MaterialDocResp materialDoc = adminMaterialDocService.getMaterialDocById(id);
        return ResultBean.success(materialDoc);
    }

    @GetMapping("/download/{id}")
    @Operation(summary = "下载教材文档")
    @SaCheckPermission("teacher:admin:material-doc:download")
    public void downloadMaterialDoc(@Parameter(description = "教材文档ID") @PathVariable @NotNull(message = "教材文档ID不能为空") Long id,
                                    HttpServletResponse response) throws IOException {
        adminMaterialDocService.downloadMaterialDoc(id, response);
    }

    @PostMapping("/parse/{id}")
    @Operation(summary = "解析教材文档")
    @SaCheckPermission("teacher:admin:material-doc:parse")
    public ResultBean<Boolean> parseMaterialDoc(@Parameter(description = "教材文档ID") @PathVariable @NotNull(message = "教材文档ID不能为空") Long id) {
        boolean result = adminMaterialDocService.parseMaterialDoc(id);
        return ResultBean.success(result);
    }

    @PostMapping
    @Operation(summary = "上传教材文档")
    @SaCheckPermission("teacher:admin:material-doc:upload")
    public ResultBean<Boolean> uploadMaterialDoc(MaterialDocSaveReq req) {
        boolean result = adminMaterialDocService.addMaterialDoc(req);
        return ResultBean.success(result);
    }

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

    @DeleteMapping("/{id}")
    @Operation(summary = "删除教材文档")
    @SaCheckPermission("teacher:admin:material-doc:delete")
    public ResultBean<Boolean> deleteMaterialDoc(@Parameter(description = "教材文档ID") @PathVariable @NotNull(message = "教材文档ID不能为空") Integer id) {
        boolean result = adminMaterialDocService.deleteMaterialDoc(id);
        return ResultBean.success(result);
    }

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