package com.storlead.mail.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.storlead.framework.common.constant.CommonConstant;
import com.storlead.framework.util.LoginUserUtil;
import com.storlead.framework.common.result.Result;
import com.storlead.mail.pojo.entity.EmailBlacklistRecordEntity;
import com.storlead.mail.pojo.entity.EmailsEntity;
import com.storlead.mail.pojo.entity.SmtpPopSettingsEntity;
import com.storlead.mail.enums.EmailBoxEnum;
import com.storlead.mail.pojo.dto.EmailTemplatesDTO;
import com.storlead.mail.service.EmailBlacklistRecordService;
import com.storlead.mail.service.EmailsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Objects;

/**
 * @program: sp-sales-platform
 * @description:
 * @author: chenkq
 * @create: 2024-10-31 10:51
 */
@RestController
@RequestMapping("/email/blacklist")
@Api(tags = "邮件: 黑名单")
@Log4j2
public class MailBlacklistRecordApiController {

    @Resource
    private EmailBlacklistRecordService blacklistRecordService;

    @Resource
    private EmailsService emailsService;

    @PostMapping(value = "/page_list")
    @ApiOperation(value = "获取邮件模板" )
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = SmtpPopSettingsEntity.class)
    })
    public Result<?> pageList(EmailTemplatesDTO dto) {
        Long currentUserId = LoginUserUtil.getCurrentUserId();
        Page<EmailBlacklistRecordEntity> page = new Page<>(dto.getPageIndex(),dto.getPageSize());
        LambdaQueryWrapper<EmailBlacklistRecordEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(EmailBlacklistRecordEntity::getOwnerBy,currentUserId);
        queryWrapper.eq(EmailBlacklistRecordEntity::getIsDelete,Integer.valueOf(0));
        IPage<EmailBlacklistRecordEntity> pageList = blacklistRecordService.page(page,queryWrapper);
        return Result.result(pageList);
    }

    @PostMapping(value = "/list")
    @ApiOperation(value = "获取邮件模板" )
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = SmtpPopSettingsEntity.class)
    })
    public Result<?> list(EmailTemplatesDTO dto) {

        Long currentUserId = LoginUserUtil.getCurrentUserId();
        Page<EmailBlacklistRecordEntity> page = new Page<>(dto.getPageIndex(),dto.getPageSize());
        LambdaQueryWrapper<EmailBlacklistRecordEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(EmailBlacklistRecordEntity::getOwnerBy,currentUserId);
        queryWrapper.eq(EmailBlacklistRecordEntity::getIsDelete,Integer.valueOf(0));
        queryWrapper.eq(EmailBlacklistRecordEntity::getEnabled,dto.getEnabled());
        IPage<EmailBlacklistRecordEntity> pageList = blacklistRecordService.page(page,queryWrapper);
        return Result.result(pageList);
    }

    @PostMapping(value = "/save")
    @ApiOperation(value = "保存模板" )
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = SmtpPopSettingsEntity.class)
    })
    public Result<?> save(EmailBlacklistRecordEntity entity) {
        Long currentUserId = LoginUserUtil.getCurrentUserId();
        if (StrUtil.isBlank(entity.getEmailAddress())) {
            return Result.error("邮箱不能为空");
        }
        LambdaQueryWrapper<EmailBlacklistRecordEntity> wrapper =new LambdaQueryWrapper<>();
        wrapper.eq(EmailBlacklistRecordEntity::getEmailAddress,entity.getEmailAddress());
        wrapper.eq(EmailBlacklistRecordEntity::getIsDelete, CommonConstant.DEL_FLAG_0);
        wrapper.eq(EmailBlacklistRecordEntity::getOwnerBy,currentUserId);
        wrapper.last(" limit 1");
        EmailBlacklistRecordEntity record = blacklistRecordService.getOne(wrapper);
        if (record != null){
            return Result.error("该邮箱已在黑名单中已存在");
        }
        Boolean b = blacklistRecordService.saveOrUpdate(entity);
        if (b) {
            /** 将黑名单邮箱的邮件移到垃圾箱中 **/
            LambdaUpdateWrapper<EmailsEntity> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(EmailsEntity::getOwnerBy,currentUserId);
            updateWrapper.eq(EmailsEntity::getFolder, EmailBoxEnum.INBOX);
            updateWrapper.eq(EmailsEntity::getFrom,entity.getEmailAddress());
            updateWrapper.set(EmailsEntity::getIsTrash,1);
            emailsService.update(updateWrapper);
        }
        return Result.ok();
    }

    @ApiOperation(value = "删除")
    @PostMapping("deleteIds")
    public Result delete(Long [] ids) {
        if(Objects.isNull(ids)) {
            return Result.error("参数错误");
        }
        blacklistRecordService.lgDelete(Arrays.asList(ids));
        return Result.ok();
    }


    @ApiOperation("禁用或启用")
    @PostMapping("enabled")
    public Result enabled(Long id,Boolean enable) {
        LambdaUpdateWrapper<EmailBlacklistRecordEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(EmailBlacklistRecordEntity::getEnabled,enable);
        updateWrapper.eq(EmailBlacklistRecordEntity::getId,id);
        blacklistRecordService.update(updateWrapper);
        return Result.ok();
    }
}
