package com.wzj.soopin.im.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzj.soopin.im.domain.bo.SysMessageTemplateBo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.web.core.BaseController;
import com.wzj.soopin.im.converter.SysMessageTemplateConvert;
import com.wzj.soopin.im.domain.SysMessageTemplate;
import com.wzj.soopin.im.domain.vo.SysMessageTemplateVo;
import com.wzj.soopin.im.service.ISysMessageTemplateService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消息模板管理
 *
 * @author ruoyi
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/im/message/template")
public class IMMessageTemplateController extends BaseController {

    private final ISysMessageTemplateService templateService;

    private  final SysMessageTemplateConvert convert;

    /**
     * 查询消息模板列表
     */
//    @SaCheckPermission("message:template:list")
    @Tag(name = "查询消息模板列表")
    @PostMapping("/page")
    public R<Page<SysMessageTemplateVo>> list(@RequestBody SysMessageTemplateBo bo, @RequestBody Page<SysMessageTemplate> page) {
        Page<SysMessageTemplate> templatePage = templateService.page(page, bo.toWrapper());
        return R.ok(convert.toVO(templatePage));
    }

    /**
     * 获取消息模板详细信息
     *
     * @param id 消息模板ID
     */
//    @SaCheckPermission("message:template:query")
    @GetMapping("/{id}")
    public R<SysMessageTemplateVo> getInfo(@NotNull(message = "消息模板ID不能为空") @PathVariable Long id) {
        SysMessageTemplate template = templateService.getById(id);
        return R.ok(convert.toVO(template));
    }

    /**
     * 根据名称查询消息模板列表（不分页）
     *
     * @param name 模板名称（可选，支持模糊查询）
     */
//    @SaCheckPermission("message:template:query")
    @Operation(summary = "根据名称查询消息模板列表（不分页）")
    @GetMapping("/listByName")
    public R<List<SysMessageTemplateVo>> listByName(@RequestParam(value = "name", required = false) String[] name) {
        String searchName = null;
        if (name != null && name.length > 0) {
            searchName = name[0];
        }
        return R.ok(templateService.selectTemplateListByName(searchName));
    }

    /**
     * 新增消息模板
     */
//    @SaCheckPermission("message:template:add")
    @Log(title = "消息模板管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/add")
    public R<Boolean> add(@Validated(AddGroup.class) @RequestBody SysMessageTemplateBo bo) {
        return R.ok(templateService.save(convert.toPo(bo)));
    }

    /**
     * 修改消息模板
     */
//    @SaCheckPermission("message:template:edit")
    @Log(title = "消息模板管理", businessType = BusinessType.UPDATE)
    @PostMapping(("/update"))
    public R<Boolean> edit(@Validated(EditGroup.class) @RequestBody SysMessageTemplateBo bo) {
        return R.ok(templateService.updateById(convert.toPo(bo)));
    }

    /**
     * 删除消息模板
     *
     * @param id 消息模板ID
     */
//    @SaCheckPermission("message:template:remove")
    @Log(title = "消息模板管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Boolean> remove(@NotEmpty(message = "消息模板ID不能为空") @PathVariable Long id) {
        return R.ok(templateService.removeById(id));
    }
}
