package com.auth.service.base.controller;

import com.auth.common.model.common.result.PageResult;
import com.auth.common.model.common.result.Result;
import com.auth.common.model.common.result.ResultCodeEnum;
import com.auth.dao.base.entity.EmailTemplateEntity;
import com.auth.model.base.dto.EmailTemplateDto;
import com.auth.model.base.vo.EmailTemplateVo;
import com.auth.service.base.service.EmailTemplateService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 邮件模板表 前端控制器
 * </p>
 *
 * @author AuthoritySystem
 * @since 2025-07-22 23:14:35
 */
@Tag(name = "邮件模板表", description = "邮件模板表相关接口")
@RestController
@RequestMapping("/api/v1/base/email-template")
@RequiredArgsConstructor
public class EmailTemplateController {

    private final EmailTemplateService emailTemplateService;

    @PreAuthorize("auth.decide('emailTemplate:query')")
    @Operation(summary = "分页查询邮件模板表", description = "分页查询邮件模板表")
    @GetMapping()
    public Result<PageResult<EmailTemplateVo>> getEmailTemplatePage(EmailTemplateDto dto) {
        Page<EmailTemplateEntity> pageParams = new Page<>(dto.getPageIndex(), dto.getPageSize());
        PageResult<EmailTemplateVo> pageResult = emailTemplateService.getEmailTemplatePage(pageParams, dto);
        return Result.success(pageResult);
    }

    @PreAuthorize("auth.decide('emailTemplate:query')")
    @Operation(summary = "根据id查询邮件模板表详情", description = "根据id查询邮件模板表详情")
    @GetMapping("{id}")
    public Result<EmailTemplateVo> getById(@PathVariable("id") Long id) {
        EmailTemplateVo emailTemplateVo = emailTemplateService.getById(id);

        return Result.success(emailTemplateVo);
    }

    @PreAuthorize("auth.decide('emailTemplate:create')")
    @Operation(summary = "添加邮件模板表", description = "添加邮件模板表")
    @PostMapping()
    public Result<String> create(@Valid @RequestBody EmailTemplateDto dto) {
        emailTemplateService.create(dto);
        return Result.success(ResultCodeEnum.ADD_SUCCESS);
    }

    @PreAuthorize("auth.decide('emailTemplate:update')")
    @Operation(summary = "更新邮件模板表", description = "更新邮件模板表")
    @PutMapping()
    public Result<String> update(@Valid @RequestBody EmailTemplateDto dto) {
        emailTemplateService.update(dto);
        return Result.success(ResultCodeEnum.UPDATE_SUCCESS);
    }

    @PreAuthorize("auth.decide('emailTemplate:delete')")
    @Operation(summary = "删除邮件模板表", description = "删除邮件模板表")
    @DeleteMapping()
    public Result<String> batchDelete(@RequestBody List<Long> ids) {
        emailTemplateService.batchDelete(ids);
        return Result.success(ResultCodeEnum.DELETE_SUCCESS);
    }
}