package com.seer.teach.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.seer.teach.admin.request.AdminConvertQuestionToHtmlReq;
import com.seer.teach.admin.request.QuestionPreviewReq;
import com.seer.teach.admin.request.RichTextTemplatePageReq;
import com.seer.teach.admin.request.RichTextTemplateQueryReq;
import com.seer.teach.admin.request.RichTextTemplateReq;
import com.seer.teach.admin.response.RichTextTemplateResp;
import com.seer.teach.admin.service.AdminRichTextTemplateService;
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 lombok.RequiredArgsConstructor;
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 java.util.List;

/**
 * 富文本模板控制器
 *
 * @author ZS
 * @since 2025-10-30
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/teacher/rich-text-template")
@Tag(name = "管理端 - 富文本模板管理")
public class AdminRichTextTemplateController {

    private final AdminRichTextTemplateService adminRichTextTemplateService;

    @PostMapping("/preview")
    @Operation(summary = "预览题目")
    @SaCheckPermission("teacher:admin:rich-text-template:preview")
    public ResultBean<String> preview(@RequestBody @Valid QuestionPreviewReq requestParam) {
        return ResultBean.success(adminRichTextTemplateService.preview(requestParam));
    }
    
    @PostMapping("/convert-to-html")
    @Operation(summary = "将题目转换为富文本")
    @SaCheckPermission("teacher:admin:rich-text-template:convert")
    public ResultBean<String> convertToHtml(@RequestBody @Valid QuestionPreviewReq requestParam) {
        return ResultBean.success(adminRichTextTemplateService.convertToHtml(requestParam));
    }

    @PostMapping("/{id}/convert-all-to-html")
    @Operation(summary = "将全部题目转换为富文本")
    @SaCheckPermission("teacher:admin:rich-text-template:convert-all")
    public ResultBean<String> convertAllExampleQuestionsToHtmlById(@PathVariable("id") Integer id,@RequestBody AdminConvertQuestionToHtmlReq convertQuestionToHtmlReq) {
        return ResultBean.success(adminRichTextTemplateService.convertAllExampleQuestionsToHtmlById(id,convertQuestionToHtmlReq));
    }

    @PostMapping("/paged")
    @Operation(summary = "分页查询富文本模板")
    @SaCheckPermission("teacher:admin:rich-text-template:list")
    public ResultBean<PageListBean<RichTextTemplateResp>> pageList(@RequestBody RichTextTemplatePageReq pageRequest) {
        return ResultBean.success(adminRichTextTemplateService.pageList(pageRequest));
    }

    @PostMapping("/list")
    @Operation(summary = "获取富文本模板列表")
    @SaCheckPermission("teacher:admin:rich-text-template:list")
    public ResultBean<List<RichTextTemplateResp>> list(@RequestBody RichTextTemplateQueryReq queryRequest) {
        return ResultBean.success(adminRichTextTemplateService.list(queryRequest));
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取富文本模板")
    @SaCheckPermission("teacher:admin:rich-text-template:detail")
    public ResultBean<RichTextTemplateResp> getById(@PathVariable Integer id) {
        return ResultBean.success(adminRichTextTemplateService.getDetail(id));
    }

    @PostMapping
    @Operation(summary = "新增富文本模板")
    @SaCheckPermission("teacher:admin:rich-text-template:save")
    public ResultBean<Boolean> save(@RequestBody RichTextTemplateReq requestParam) {
        return ResultBean.success(adminRichTextTemplateService.save(requestParam));
    }

    @PutMapping
    @Operation(summary = "更新富文本模板")
    @SaCheckPermission("teacher:admin:rich-text-template:update")
    public ResultBean<Boolean> update(@RequestBody RichTextTemplateReq requestParam) {
        return ResultBean.success(adminRichTextTemplateService.updateById(requestParam));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除富文本模板")
    @SaCheckPermission("teacher:admin:rich-text-template:delete")
    public ResultBean<Boolean> delete(@PathVariable Integer id) {
        return ResultBean.success(adminRichTextTemplateService.deleteById(id));
    }
}