package org.xyzh.crontab.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xyzh.api.crontab.EmailRecipientService;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.core.page.PageRequest;
import org.xyzh.common.dto.crontab.TbCrontabEmailRecipient;

import java.util.List;

/**
 * @description 定时任务邮件接收人控制器
 * @filename EmailRecipientController.java
 * @author yslg
 * @copyright xyzh
 * @since 2025-11-18
 */
@RestController
@RequestMapping("/crontab/email/recipient")
public class EmailRecipientController {

    private static final Logger logger = LoggerFactory.getLogger(EmailRecipientController.class);

    @Autowired
    private EmailRecipientService emailRecipientService;

    /**
     * @description 创建邮件接收人
     * @param recipient 邮件接收人
     * @return ResultDomain<TbCrontabEmailRecipient>
     */
    @PostMapping
    public ResultDomain<TbCrontabEmailRecipient> createRecipient(@RequestBody TbCrontabEmailRecipient recipient) {
        return emailRecipientService.createRecipient(recipient);
    }

    /**
     * @description 批量创建邮件接收人
     * @param recipients 邮件接收人列表
     * @return ResultDomain<Boolean>
     */
    @PostMapping("/batch")
    public ResultDomain<Boolean> batchCreateRecipient(@RequestBody List<TbCrontabEmailRecipient> recipients) {
        return emailRecipientService.batchCreateRecipient(recipients);
    }

    /**
     * @description 更新邮件接收人
     * @param recipient 邮件接收人
     * @return ResultDomain<TbCrontabEmailRecipient>
     */
    @PutMapping
    public ResultDomain<TbCrontabEmailRecipient> updateRecipient(@RequestBody TbCrontabEmailRecipient recipient) {
        return emailRecipientService.updateRecipient(recipient);
    }

    /**
     * @description 删除邮件接收人
     * @param recipientId 接收人ID
     * @return ResultDomain<Boolean>
     */
    @DeleteMapping("/{recipientId}")
    public ResultDomain<Boolean> deleteRecipient(@PathVariable String recipientId) {
        return emailRecipientService.deleteRecipient(recipientId);
    }

    /**
     * @description 根据ID查询
     * @param recipientId 接收人ID
     * @return ResultDomain<TbCrontabEmailRecipient>
     */
    @GetMapping("/{recipientId}")
    public ResultDomain<TbCrontabEmailRecipient> getRecipientById(@PathVariable String recipientId) {
        return emailRecipientService.getRecipientById(recipientId);
    }

    /**
     * @description 根据任务ID查询接收人列表
     * @param taskId 任务ID
     * @return ResultDomain<TbCrontabEmailRecipient>
     */
    @GetMapping("/task/{taskId}")
    public ResultDomain<TbCrontabEmailRecipient> getRecipientsByTaskId(@PathVariable String taskId) {
        return emailRecipientService.getRecipientsByTaskId(taskId);
    }

    /**
     * @description 分页查询邮件接收人
     * @param pageRequest 分页请求
     * @return ResultDomain<TbCrontabEmailRecipient>
     */
    @PostMapping("/page")
    public ResultDomain<TbCrontabEmailRecipient> getRecipientPage(@RequestBody PageRequest<TbCrontabEmailRecipient> pageRequest) {
        return emailRecipientService.getRecipientPage(pageRequest);
    }

    /**
     * @description 删除任务的所有接收人
     * @param taskId 任务ID
     * @return ResultDomain<Boolean>
     */
    @DeleteMapping("/task/{taskId}")
    public ResultDomain<Boolean> deleteRecipientsByTaskId(@PathVariable String taskId) {
        return emailRecipientService.deleteRecipientsByTaskId(taskId);
    }
}
