package com.yushu.web.controller.system;

import com.yushu.common.annotation.Log;
import com.yushu.common.core.controller.BaseController;
import com.yushu.common.core.domain.AjaxResult;
import com.yushu.common.core.page.TableDataInfo;
import com.yushu.common.enums.BusinessType;
import com.yushu.system.domain.SysMailInbox;
import com.yushu.system.service.ISysMailInboxService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 收件箱Controller
 * 
 * @author yushu
 * @date 2024-12-13
 */
@RestController
@RequestMapping("/system/mail/inbox")
public class SysMailInboxController extends BaseController {

    @Autowired
    private ISysMailInboxService sysMailInboxService;

    /**
     * 查询收件列表
     */
    @PreAuthorize("@ss.hasPermi('system:mail:inbox:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysMailInbox sysMailInbox) {
        startPage();
        List<SysMailInbox> list = sysMailInboxService.selectSysMailInboxList(sysMailInbox);
        return getDataTable(list);
    }

    /**
     * 获取收件详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:mail:inbox:query')")
    @GetMapping(value = "/{inboxId}")
    public AjaxResult getInfo(@PathVariable("inboxId") Long inboxId) {
        // 标记为已读
        sysMailInboxService.markAsRead(inboxId);
        return success(sysMailInboxService.selectSysMailInboxByInboxId(inboxId));
    }

    /**
     * 获取未读数量
     */
    @GetMapping("/unreadCount")
    public AjaxResult unreadCount(@RequestParam(required = false) Long configId) {
        return success(sysMailInboxService.selectUnreadCount(configId));
    }

    /**
     * 标记已读
     */
    @PreAuthorize("@ss.hasPermi('system:mail:inbox:query')")
    @PutMapping("/markRead/{inboxId}")
    public AjaxResult markRead(@PathVariable Long inboxId) {
        return toAjax(sysMailInboxService.markAsRead(inboxId));
    }

    /**
     * 批量标记已读
     */
    @PreAuthorize("@ss.hasPermi('system:mail:inbox:query')")
    @PutMapping("/batchMarkRead")
    public AjaxResult batchMarkRead(@RequestBody Long[] inboxIds) {
        return toAjax(sysMailInboxService.batchMarkAsRead(inboxIds));
    }

    /**
     * 切换星标
     */
    @PreAuthorize("@ss.hasPermi('system:mail:inbox:query')")
    @PutMapping("/toggleStar/{inboxId}")
    public AjaxResult toggleStar(@PathVariable Long inboxId) {
        return toAjax(sysMailInboxService.toggleStar(inboxId));
    }

    /**
     * 删除收件
     */
    @PreAuthorize("@ss.hasPermi('system:mail:inbox:remove')")
    @Log(title = "收件箱", businessType = BusinessType.DELETE)
    @DeleteMapping("/{inboxIds}")
    public AjaxResult remove(@PathVariable Long[] inboxIds) {
        return toAjax(sysMailInboxService.deleteSysMailInboxByInboxIds(inboxIds));
    }

    /**
     * 同步邮件
     */
    @PreAuthorize("@ss.hasPermi('system:mail:inbox:sync')")
    @Log(title = "收件箱", businessType = BusinessType.OTHER)
    @PostMapping("/sync/{configId}")
    public AjaxResult sync(@PathVariable Long configId) {
        try {
            int count = sysMailInboxService.syncMails(configId);
            return AjaxResult.success("同步完成", count);
        } catch (Exception e) {
            logger.error("同步邮件失败", e);
            return error("同步失败: " + e.getMessage());
        }
    }

    /**
     * 标记指定账户的所有未读邮件为已读
     */
    @PreAuthorize("@ss.hasPermi('system:mail:inbox:query')")
    @PutMapping("/markAllAsRead/{configId}")
    public AjaxResult markAllAsRead(@PathVariable Long configId) {
        return toAjax(sysMailInboxService.markAllAsReadByConfig(configId));
    }

}
